| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2013 Thomas Anderson <blobfish[at]gmx.com> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include "ArcEngine.h" |
| 24 |
#include <limits> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include <Inventor/engines/SoCalculator.h> |
| 28 |
#include <Inventor/engines/SoComposeVec3f.h> |
| 29 |
#include <Inventor/engines/SoConcatenate.h> |
| 30 |
#include <Inventor/engines/SoComposeRotation.h> |
| 31 |
#include <Inventor/engines/SoComposeRotationFromTo.h> |
| 32 |
|
| 33 |
#include <Inventor/nodekits/SoShapeKit.h> |
| 34 |
#include <Inventor/nodes/SoCone.h> |
| 35 |
#include <Inventor/nodes/SoFont.h> |
| 36 |
#include <Inventor/nodes/SoLineSet.h> |
| 37 |
#include <Inventor/nodes/SoMaterial.h> |
| 38 |
#include <Inventor/nodes/SoMatrixTransform.h> |
| 39 |
#include <Inventor/nodes/SoResetTransform.h> |
| 40 |
#include <Inventor/nodes/SoSeparator.h> |
| 41 |
#include <Inventor/nodes/SoTransform.h> |
| 42 |
#include <Inventor/nodes/SoVertexProperty.h> |
| 43 |
|
| 44 |
using namespace Gui; |
| 45 |
|
| 46 |
|
| 47 |
SO_ENGINE_SOURCE(ArcEngine) |
| 48 |
|
| 49 |
ArcEngine::ArcEngine() |
| 50 |
{ |
| 51 |
SO_ENGINE_CONSTRUCTOR(ArcEngine); |
| 52 |
|
| 53 |
SO_ENGINE_ADD_INPUT(radius, (10.0)); |
| 54 |
SO_ENGINE_ADD_INPUT(angle, (1.0)); |
| 55 |
SO_ENGINE_ADD_INPUT(deviation, (0.25)); |
| 56 |
|
| 57 |
SO_ENGINE_ADD_OUTPUT(points, SoMFVec3f); |
| 58 |
SO_ENGINE_ADD_OUTPUT(pointCount, SoSFInt32); |
| 59 |
SO_ENGINE_ADD_OUTPUT(midpoint, SoSFVec3f); |
| 60 |
} |
| 61 |
|
| 62 |
void ArcEngine::initClass() |
| 63 |
{ |
| 64 |
SO_ENGINE_INIT_CLASS(ArcEngine, SoEngine, "Engine"); |
| 65 |
} |
| 66 |
|
| 67 |
void ArcEngine::evaluate() |
| 68 |
{ |
| 69 |
float angle = abs(this->angle.getValue()); |
| 70 |
|
| 71 |
if (radius.getValue() < std::numeric_limits<float>::epsilon() |
| 72 |
|| deviation.getValue() < std::numeric_limits<float>::epsilon()) { |
| 73 |
defaultValues(); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
float deviationAngle(acos((radius.getValue() - deviation.getValue()) / radius.getValue())); |
| 78 |
std::vector<SbVec3f> tempPoints; |
| 79 |
int segmentCount; |
| 80 |
if (deviationAngle >= angle) { |
| 81 |
segmentCount = 1; |
| 82 |
} |
| 83 |
else { |
| 84 |
segmentCount = static_cast<int>(angle / deviationAngle) + 1; |
| 85 |
if (segmentCount < 2) { |
| 86 |
defaultValues(); |
| 87 |
return; |
| 88 |
} |
| 89 |
} |
| 90 |
float angleIncrement = (this->angle.getValue() > 0 ? angle : -angle) |
| 91 |
/ static_cast<float>(segmentCount); |
| 92 |
for (int index = 0; index < segmentCount + 1; ++index) { |
| 93 |
SbVec3f currentNormal(1.0, 0.0, 0.0); |
| 94 |
float currentAngle = index * angleIncrement; |
| 95 |
SbRotation rotation(SbVec3f(0.0, 0.0, 1.0), currentAngle); |
| 96 |
rotation.multVec(currentNormal, currentNormal); |
| 97 |
tempPoints.push_back(currentNormal * radius.getValue()); |
| 98 |
} |
| 99 |
int tempCount = tempPoints.size(); // for macro. |
| 100 |
SO_ENGINE_OUTPUT(points, SoMFVec3f, setNum(tempCount)); |
| 101 |
SO_ENGINE_OUTPUT(pointCount, SoSFInt32, setValue(tempCount)); |
| 102 |
std::vector<SbVec3f>::const_iterator it; |
| 103 |
for (it = tempPoints.begin(); it != tempPoints.end(); ++it) { |
| 104 |
int currentIndex = it - tempPoints.begin(); // for macro. |
| 105 |
SbVec3f temp(*it); // for macro |
| 106 |
SO_ENGINE_OUTPUT(points, SoMFVec3f, set1Value(currentIndex, temp)); |
| 107 |
} |
| 108 |
|
| 109 |
// Get Midpoint |
| 110 |
float a = angle / 2; |
| 111 |
SbRotation rot(SbVec3f(0.0, 0.0, 1.0), a); |
| 112 |
SbVec3f midPnt(1.0, 0.0, 0.0); |
| 113 |
rot.multVec(midPnt, midPnt); |
| 114 |
midPnt = midPnt * radius.getValue(); |
| 115 |
|
| 116 |
SO_ENGINE_OUTPUT(midpoint, SoSFVec3f, setValue(midPnt)); |
| 117 |
} |
| 118 |
|
| 119 |
void ArcEngine::defaultValues() |
| 120 |
{ |
| 121 |
// just some non-failing info. |
| 122 |
SO_ENGINE_OUTPUT(points, SoMFVec3f, setNum(2)); |
| 123 |
SbVec3f point1(10.0, 0.0, 0.0); |
| 124 |
SO_ENGINE_OUTPUT(points, SoMFVec3f, set1Value(0, point1)); |
| 125 |
SbVec3f point2(7.07f, 7.07f, 0.0); |
| 126 |
SO_ENGINE_OUTPUT(points, SoMFVec3f, set1Value(1, point2)); |
| 127 |
SO_ENGINE_OUTPUT(pointCount, SoSFInt32, setValue(2)); |
| 128 |
SbVec3f point3(7.07f, 7.07f, 0.0); |
| 129 |
SO_ENGINE_OUTPUT(midpoint, SoSFVec3f, setValue(point3)); |
| 130 |
} |