FreeCAD

Форк
0
/
ArcEngine.cpp 
136 строк · 4.9 Кб
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 "PreCompiled.h"
24

25
#include "ArcEngine.h"
26
#include <limits>
27
#include <vector>
28

29
# include <Inventor/engines/SoCalculator.h>
30
# include <Inventor/engines/SoComposeVec3f.h>
31
# include <Inventor/engines/SoConcatenate.h>
32
# include <Inventor/engines/SoComposeRotation.h>
33
# include <Inventor/engines/SoComposeRotationFromTo.h>
34

35
# include <Inventor/nodekits/SoShapeKit.h>
36
# include <Inventor/nodes/SoCone.h>
37
# include <Inventor/nodes/SoFont.h>
38
# include <Inventor/nodes/SoLineSet.h>
39
# include <Inventor/nodes/SoMaterial.h>
40
# include <Inventor/nodes/SoMatrixTransform.h>
41
# include <Inventor/nodes/SoResetTransform.h>
42
# include <Inventor/nodes/SoSeparator.h>
43
# include <Inventor/nodes/SoTransform.h>
44
# include <Inventor/nodes/SoVertexProperty.h>
45

46
using namespace Gui;
47

48

49

50
SO_ENGINE_SOURCE(ArcEngine)
51

52
ArcEngine::ArcEngine()
53
{
54
  SO_ENGINE_CONSTRUCTOR(ArcEngine);
55

56
  SO_ENGINE_ADD_INPUT(radius, (10.0));
57
  SO_ENGINE_ADD_INPUT(angle, (1.0));
58
  SO_ENGINE_ADD_INPUT(deviation, (0.25));
59

60
  SO_ENGINE_ADD_OUTPUT(points, SoMFVec3f);
61
  SO_ENGINE_ADD_OUTPUT(pointCount, SoSFInt32);
62
  SO_ENGINE_ADD_OUTPUT(midpoint, SoSFVec3f);
63
}
64

65
void ArcEngine::initClass()
66
{
67
  SO_ENGINE_INIT_CLASS(ArcEngine, SoEngine, "Engine");
68
}
69

70
void ArcEngine::evaluate()
71
{
72
  float angle = abs(this->angle.getValue());
73

74
  if (radius.getValue() < std::numeric_limits<float>::epsilon() ||
75
    deviation.getValue() < std::numeric_limits<float>::epsilon())
76
  {
77
    defaultValues();
78
    return;
79
  }
80

81
  float deviationAngle(acos((radius.getValue() - deviation.getValue()) / radius.getValue()));
82
  std::vector<SbVec3f> tempPoints;
83
  int segmentCount;
84
  if (deviationAngle >= angle) {
85
    segmentCount = 1;
86
  }
87
  else {
88
    segmentCount = static_cast<int>(angle / deviationAngle) + 1;
89
    if (segmentCount < 2) {
90
      defaultValues();
91
      return;
92
    }
93
  }
94
  float angleIncrement = (this->angle.getValue() > 0 ? angle : -angle) / static_cast<float>(segmentCount);
95
  for (int index = 0; index < segmentCount + 1; ++index)
96
  {
97
    SbVec3f currentNormal(1.0, 0.0, 0.0);
98
    float currentAngle = index * angleIncrement;
99
    SbRotation rotation(SbVec3f(0.0, 0.0, 1.0), currentAngle);
100
    rotation.multVec(currentNormal, currentNormal);
101
    tempPoints.push_back(currentNormal * radius.getValue());
102
  }
103
  int tempCount = tempPoints.size(); //for macro.
104
  SO_ENGINE_OUTPUT(points, SoMFVec3f, setNum(tempCount));
105
  SO_ENGINE_OUTPUT(pointCount, SoSFInt32, setValue(tempCount));
106
  std::vector<SbVec3f>::const_iterator it;
107
  for (it = tempPoints.begin(); it != tempPoints.end(); ++it)
108
  {
109
    int currentIndex = it-tempPoints.begin(); //for macro.
110
    SbVec3f temp(*it); //for macro
111
    SO_ENGINE_OUTPUT(points, SoMFVec3f, set1Value(currentIndex, temp));
112
  }
113

114
  // Get Midpoint
115
  float a = angle / 2;
116
  SbRotation rot(SbVec3f(0.0, 0.0, 1.0), a);
117
  SbVec3f midPnt(1.0, 0.0, 0.0);
118
  rot.multVec(midPnt, midPnt);
119
  midPnt = midPnt * radius.getValue();
120

121
  SO_ENGINE_OUTPUT(midpoint, SoSFVec3f, setValue(midPnt));
122

123
}
124

125
void ArcEngine::defaultValues()
126
{
127
  //just some non-failing info.
128
  SO_ENGINE_OUTPUT(points, SoMFVec3f, setNum(2));
129
  SbVec3f point1(10.0, 0.0, 0.0);
130
  SO_ENGINE_OUTPUT(points, SoMFVec3f, set1Value(0, point1));
131
  SbVec3f point2(7.07f, 7.07f, 0.0);
132
  SO_ENGINE_OUTPUT(points, SoMFVec3f, set1Value(1, point2));
133
  SO_ENGINE_OUTPUT(pointCount, SoSFInt32, setValue(2));
134
  SbVec3f point3(7.07f, 7.07f, 0.0);
135
  SO_ENGINE_OUTPUT(midpoint, SoSFVec3f, setValue(point3));
136
}
137

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.