PolyLinePlugin_Circle.cc 8.12 KB
Newer Older
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Martin Schultz's avatar
Martin Schultz committed
4 5 6 7
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
8 9
 *                                                                           *
 *---------------------------------------------------------------------------*
Martin Schultz's avatar
Martin Schultz committed
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
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. 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.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder 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.              *
39
 *                                                                           *
40
 \*===========================================================================*/
41 42 43 44 45 46 47

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                       *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
48
 \*===========================================================================*/
49 50 51 52 53 54 55 56 57 58 59 60 61 62

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

//== INCLUDES =================================================================
#include "PolyLinePlugin.hh"

namespace {

struct Onb {

63 64
        ACG::Vec3d x, y, z;

Jan Möbius's avatar
Jan Möbius committed
65
        explicit Onb(PolyLineCircleData* _circleData) {
66 67 68 69 70 71 72 73 74 75
            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]);
        }
76 77 78 79 80 81
};

}

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

82 83 84
bool PolyLinePlugin::createCircle_getPointOnMesh(TriMeshObject* _triMeshObject, ACG::Vec3d _center,
        ACG::Vec3d _pOnPlane, ACG::Vec3d _n, ACG::Vec3d* _pOut) {
    OpenMeshTriangleBSPT<TriMesh>* bsp = _triMeshObject->requestTriangleBsp();
Martin Schultz's avatar
Martin Schultz committed
85 86
    OpenMeshTriangleBSPT<TriMesh>::RayCollision rayInt;
    rayInt = bsp->raycollision(_pOnPlane, _n);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    if (rayInt.empty())
        return false;
    int i = -1;
    double smDist = 10e10;

    for (unsigned int j = 0; j < rayInt.size(); j++) {
        ACG::Vec3d p = _pOnPlane + _n * rayInt[j].second, dir = _center - p;
        const double dist = dir.norm();
        if (dist < smDist) {
            smDist = dist;
            if (_pOut)
                *_pOut = p; //+ norAtInt * 0.002;//that would fix lines below the surface, but what is the correct scale?
            i = j;
        }
    }

    return i != -1;
105 106 107 108
}

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

109 110 111 112 113 114 115 116 117 118 119 120 121 122
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_;
    double t = ((n | x0) - (n | _hit_Point)) / n.sqrnorm();
    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;
123 124 125 126
}

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

127 128 129 130 131 132 133
ACG::Vec3d PolyLinePlugin::createCircle_getHit(PolyLineCircleData* _circleData, ACG::Vec3d _hit_point) {
    ACG::Vec3d pOnMesh, pOnPlane;
    double r;
    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
void PolyLinePlugin::updatePolyEllipse(PolyLineObject* _lineObject, unsigned int _pointCount) {
    PolyLineCircleData* lineData = dynamic_cast<PolyLineCircleData*>(_lineObject->objectData(CIRCLE_DATA));

    TriMeshObject* mesh;
    if(!PluginFunctions::getObject(lineData->circleMeshIndex_, mesh))
        return;//if there is no mesh available do not try to recreate the circle

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

    _lineObject->line()->clear();
151
    _lineObject->line()->set_closed(tool_->rb_CloseCircle->isChecked());
152 153 154 155 156 157 158 159 160 161 162 163 164 165

    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;
        ACG::Vec3d pOnMesh;
        const ACG::Vec3d pOnPlane(y, 0, x), //this is in the local coord system!
                         pInWorld = basis.toWorld(pOnPlane) + lineData->circleCenter_;
        if(moveCircle_IsFloating)
            _lineObject->line()->add_point(pInWorld);
        else if(createCircle_getHitInfo(lineData, pInWorld, &pOnMesh))
            _lineObject->line()->add_point(pOnMesh);
    }
166

167
    emit updatedObject(_lineObject->id(), UPDATE_GEOMETRY | UPDATE_TOPOLOGY);
168 169 170 171
}

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