on the way to a generic geometry interface
[engrid.git] / src / libengrid / cadinterface.cpp
blobc13e71cc68eb25dd873a15ffccb5906b8c4649bd
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008-2013 enGits GmbH +
7 // + +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
12 // + +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
17 // + +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include "cadinterface.h"
25 #include <QTextStream>
26 #include <QFile>
28 CadInterface::CadInterface()
30 QFile file(":/resources/misc/raysphere.dat");
31 file.open(QIODevice::ReadOnly);
32 QTextStream s(&file);
33 int N;
34 s >> N;
35 m_RayPoints.resize(N);
36 for (int i = 0; i < N; ++i) {
37 for (int j = 0; j < 3; ++j) {
38 s >> m_RayPoints[i][j];
40 m_RayPoints[i].normalise();
42 m_LastRadius = 1e99;
43 m_LastNormal = vec3_t(0,0,0);
46 vec3_t CadInterface::snap(vec3_t x)
48 double L_best = 1e99;
49 double r_best;
50 vec3_t x_best;
51 vec3_t n_best;
52 bool no_hit = true;
53 foreach (vec3_t x_ray, m_RayPoints) {
54 double r_hit;
55 vec3_t x_hit;
56 vec3_t n_hit;
57 vec3_t v = x_ray;
59 // standard ray
60 if (shootRay(x, v, x_hit, n_hit, r_hit) != Miss) {
61 double L = (x_hit - x).abs();
62 if (L < L_best) {
63 x_best = x_hit;
64 n_best = n_hit;
65 r_best = r_hit;
66 L_best = L;
67 no_hit = false;
69 v = n_hit;
71 // shoot a "far-side return ray" ...
72 if (shootRay(x_hit, -1*x_ray, x_hit, n_hit, r_hit)) {
73 double L = (x_hit - x).abs();
74 if (L < L_best) {
75 x_best = x_hit;
76 n_best = n_hit;
77 r_best = r_hit;
78 L_best = L;
79 no_hit = false;
83 // re-shoot standard ray with surface normal
84 if (shootRay(x, v, x_hit, n_hit, r_hit) != Miss) {
85 double L = (x_hit - x).abs();
86 if (L < L_best) {
87 x_best = x_hit;
88 n_best = n_hit;
89 r_best = r_hit;
90 L_best = L;
91 no_hit = false;
95 // re-shoot standard ray with surface normal in opposite direction
96 v *= -1;
97 if (shootRay(x, v, x_hit, n_hit, r_hit) != Miss) {
98 double L = (x_hit - x).abs();
99 if (L < L_best) {
100 x_best = x_hit;
101 n_best = n_hit;
102 r_best = r_hit;
103 L_best = L;
104 no_hit = false;
110 if (no_hit) {
111 EG_BUG;
113 m_LastNormal = n_best;
114 m_LastRadius = r_best;
115 return x_best;