/* <copyright>
Copyright (c) 2012, Motorola Mobility LLC.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Motorola Mobility LLC nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
</copyright> */
var Montage = require("montage/core/core").Montage,
Component = require("montage/ui/component").Component,
vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils,
Rectangle = require("js/helper-classes/3D/rectangle").Rectangle,
ElementsMediator = require("js/mediators/element-mediator").ElementMediator;
///////////////////////////////////////////////////////////////////////
// Class ViewUtils
// Viewing Utility functions
///////////////////////////////////////////////////////////////////////
exports.ViewUtils = Montage.create(Component, {
///////////////////////////////////////////////////////////////////////
// Instance variables
///////////////////////////////////////////////////////////////////////
m_viewportObj : { value: null},
_perspectiveDist: { value: null},
// keep a stack of viewport objects
_viewportObjStack: { value: []},
_rootElement: { value: null},
_stageElement: { value: null},
///////////////////////////////////////////////////////////////////////
// Property accessors
///////////////////////////////////////////////////////////////////////
setViewportObj: {
value: function( vp ) {
this.m_viewportObj = vp;
this._perspectiveDist = this.getPerspectiveDistFromElement( vp );
}
},
getViewportObj: { value: function() { return this.m_viewportObj; } },
setRootElement: { value: function( elt ) { this._rootElement = elt; } },
getRootElement: { value: function () { return this._rootElement; } },
setStageElement: { value: function( elt ) { this._stageElement = elt; } },
getStageElement: { value: function () { return this._stageElement; } },
getPerspectiveDistance: { value: function () { return this._perspectiveDist; } },
///////////////////////////////////////////////////////////////////////
// Camera and View Methods
///////////////////////////////////////////////////////////////////////
getMatrixFromElement: {
value: function( elt ) {
var mat = ElementsMediator.getMatrix(elt);
if(mat)
{
return mat;
}
else
{
return Matrix.I(4);
}
}
},
setMatrixForElement: {
value: function( elt, mat, isChanging ) {
ElementsMediator.setMatrix(elt, mat, isChanging);
}
},
elementHas3D: {
value: function( elt ) {
return ElementsMediator.has3D(elt);
}
},
getElementPlane: {
value: function( elt ) {
var bounds = this.getElementViewBounds3D( elt );
var xArr = new Array(), yArr = new Array(), zArr = new Array();
for (var j=0; j<4; j++)
{
var pt = this.localToGlobal( bounds[j], elt );
xArr.push(pt[0]); yArr.push(pt[1]); zArr.push(pt[2]);
}
var normal = MathUtils.getPolygonNormal( 4, xArr, yArr, zArr );
//var d = -MathUtils.dot3( bounds[0], normal );
var d = -MathUtils.dot3( [xArr[0],yArr[0],zArr[0]], normal );
normal[3] = d;
return normal;
}
},
getUnprojectedElementPlane: {
value: function( elt ) {
var mat = this.getMatrixFromElement(elt);
var plane = [mat[8], mat[9], mat[10], mat[11]];
// The translation value is a point on the plane
this.pushViewportObj( elt );
var ptOnPlane = this.getCenterOfProjection();
this.popViewportObj();
ptOnPlane[2] = 0;
ptOnPlane = this.localToStageWorld(ptOnPlane, elt);
plane[3] = -vecUtils.vecDot(3, plane, ptOnPlane );
return plane;
}
},
/*
* This method will return a normal to a plane containing the Z axis and either the
* x or y axis of the element.
*/
getNormalToUnprojectedElementPlane:
{
value: function( elt, axis, localMode )
{
var objMat = this.getMatrixFromElement(elt);
var objMatInv = glmat4.inverse( objMat, [] );
var xVec = [1,0,0];
var yVec = [0,1,0];
var zVec = [0,0,1];
var stage = this.application.ninja.currentDocument.model.documentRoot;
var stageMat = this.getMatrixFromElement(stage);
var mat = glmat4.multiply( stageMat, objMat, [] );
var viewDir;
if (localMode)
{
var matInv = glmat4.inverse( mat, [] );
viewDir = MathUtils.transformVector( [0,0,1], matInv );
}
else
{
var stageInv = glmat4.inverse( stageMat, [] );
viewDir = MathUtils.transformVector( [0,0,1], stageInv );
}
var plane;
var xDot, yDot, zDot;
switch (axis)
{
case 0:
yDot = Math.abs(vecUtils.vecDot(3, yVec, viewDir));
zDot = Math.abs(vecUtils.vecDot(3, zVec, viewDir));
if(yDot > zDot)
plane = vecUtils.vecCross( 3, zVec, xVec );
else
plane = vecUtils.vecCross( 3, yVec, xVec );
break;
case 1:
xDot = Math.abs(vecUtils.vecDot(3, yVec, viewDir));
zDot = Math.abs(vecUtils.vecDot(3, zVec, viewDir));
if(xDot > zDot)
plane = vecUtils.vecCross( 3, zVec, yVec );
else
plane = vecUtils.vecCross( 3, xVec, yVec );
break;
break;
case 2:
xDot = Math.abs(vecUtils.vecDot(3, xVec, viewDir));
yDot = Math.abs(vecUtils.vecDot(3, yVec, viewDir));
if(xDot > yDot)
plane = vecUtils.vecCross( 3, yVec, zVec );
else
plane = vecUtils.vecCross( 3, xVec, zVec );
break;
}
if (localMode) plane = MathUtils.transformVector( plane, objMat );
// The translation value is a point on the plane
this.pushVi
|