PolyLinePlugin_Circle.cc 7.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                       *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/

//=============================================================================
//
//  CLASS PolyLinePlugin - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================

#include "PolyLinePlugin.hh"

namespace {

struct Onb {

	ACG::Vec3d x,y,z;

	Onb(PolyLineCircleData* _circleData) {
		x = _circleData->circleMainAxis_;
		z = _circleData->circleSideAxis_;
		y = _circleData->circleNormal_;
	}
	ACG::Vec3d toWorld(ACG::Vec3d v) const {
		return ACG::Vec3d(x[0] * v[0] + y[0] * v[1] + z[0] * v[2],
					        	  x[1] * v[0] + y[1] * v[1] + z[1] * v[2],
					        	  x[2] * v[0] + y[2] * v[1] + z[2] * v[2]);
	}
};

}

//-----------------------------------------------------------------------------

bool
PolyLinePlugin::
createCircle_getPointOnMesh(TriMeshObject* _triMeshObject, ACG::Vec3d _center, ACG::Vec3d _pOnPlane, ACG::Vec3d _n, ACG::Vec3d* _pOut)
{
	OpenMeshTriangleBSPT<TriMesh>* bsp            = _triMeshObject->requestTriangleBsp();
Jan Möbius's avatar
Jan Möbius committed
81
	OpenMeshTriangleBSPT<TriMesh>::RayCollision rayInt = bsp->raycollision(_pOnPlane, _n);
82

Jan Möbius's avatar
Jan Möbius committed
83
	if(rayInt.empty())
84 85 86 87 88
		return false;

	int i = -1;
	double smDist = 10e10;

Jan Möbius's avatar
Jan Möbius committed
89 90 91
	for(unsigned int j = 0; j < rayInt.size(); j++) {
		ACG::Vec3d norAtInt = _triMeshObject->mesh()->normal(rayInt[j].first);
		ACG::Vec3d p = _pOnPlane + _n * rayInt[j].second, dir = _center - p;
Jan Möbius's avatar
Jan Möbius committed
92
		const double dist = dir.norm();
93 94 95
		if(dist < smDist) {
			smDist = dist;
			if(_pOut)
Jan Möbius's avatar
Jan Möbius committed
96
				*_pOut = p;//+ norAtInt * 0.002;//that would fix lines below the surface, but what is the correct scale?
97 98 99 100 101 102 103 104 105 106 107 108 109 110
			i = j;
		}
	}

	return i != -1;
}

//-----------------------------------------------------------------------------

bool
PolyLinePlugin::
createCircle_getHitInfo(PolyLineCircleData* _circleData, ACG::Vec3d _hit_Point, ACG::Vec3d* _pOut, double* r, ACG::Vec3d* _onPlaneO)
{
	ACG::Vec3d n = _circleData->circleNormal_, x0 = _circleData->circleCenter_;
Jan Möbius's avatar
Jan Möbius committed
111
	double t = ((n | x0) - (n | _hit_Point)) / n.sqrnorm();
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	ACG::Vec3d onPlane = _hit_Point + t * n,  d = onPlane - x0;
	if(r)
		*r = d.norm();
	if(_onPlaneO)
		*_onPlaneO = onPlane;
	TriMeshObject* mesh;
	if(PluginFunctions::getObject(_circleData->circleMeshIndex_, mesh))
		return createCircle_getPointOnMesh(mesh, x0, onPlane, n, _pOut);
	else return false;
}

//-----------------------------------------------------------------------------

ACG::Vec3d
PolyLinePlugin::
createCircle_getHit(PolyLineCircleData* _circleData, ACG::Vec3d _hit_point)
{
Jan Möbius's avatar
Jan Möbius committed
129
	ACG::Vec3d pOnMesh, pOnPlane;
130
	double r;
Jan Möbius's avatar
Jan Möbius committed
131 132 133
	if(!createCircle_getHitInfo(_circleData, _hit_point, &pOnMesh, &r, &pOnPlane))
		return pOnPlane;//no point on the mesh was found...
	else return pOnMesh;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
}

//-----------------------------------------------------------------------------

void
PolyLinePlugin::
updatePolyEllipse(PolyLineObject* _lineObject, unsigned int _pointCount)
{
	PolyLineCircleData* lineData = dynamic_cast<PolyLineCircleData*>(_lineObject->objectData(CIRCLE_DATA));

	const double theta = 2.0 * M_PI / double(_pointCount);
	_pointCount += tool_->rb_CloseCircle->isChecked() ? 0 : -1;
	const double r2 = lineData->circleMainRadius_, r1 = lineData->circleSideRadius_;
	const Onb basis(lineData);

	_lineObject->line()->clear();

	for(unsigned int i = 0; i <= _pointCount; i++) {
		const double tanTheta_i = tan(theta * i);
		double x = (r1 * r2) / sqrt(r2 * r2 + r1 * r1 * tanTheta_i * tanTheta_i);
		x = ((theta * i) <= M_PI_2 || (theta * i) > (3.0 * M_PI_2)) ? x : -x;
		const double y = tanTheta_i * x;
Jan Möbius's avatar
Jan Möbius committed
156 157 158 159 160
		ACG::Vec3d pOnMesh;
		const ACG::Vec3d pOnPlane(y,0,x),//this is in the local coord system!
						 pInWorld = basis.toWorld(pOnPlane) + lineData->circleCenter_;
		if(createCircle_getHitInfo(lineData, pInWorld, &pOnMesh))
		  _lineObject->line()->add_point(pOnMesh);
161 162 163 164 165 166 167 168 169 170 171
	}
	emit updatedObject(_lineObject->id(), UPDATE_GEOMETRY | UPDATE_TOPOLOGY);
}

//-----------------------------------------------------------------------------

void
PolyLinePlugin::
slot_setCirclePointNum(int i)
{
  PolyLineObject* _lineObject;
Jan Möbius's avatar
Jan Möbius committed
172
  if(createCircle_LastSelIndex_ != -1 && PluginFunctions::getObject(createCircle_LastSelIndex_, _lineObject))
173 174 175 176 177
    updatePolyEllipse(_lineObject, i);
}