simplified
[engrid-github.git] / src / libengrid / brlcadinterface.cpp
bloba9300fd21a185566de2035b84003d46d432a1e26
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 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 //
24 #include "brlcadinterface.h"
26 #ifdef BRLCAD_SUPPORT
28 vec3_t BrlCadInterface::m_XIn;
29 vec3_t BrlCadInterface::m_XOut;
30 vec3_t BrlCadInterface::m_InNormal;
31 vec3_t BrlCadInterface::m_OutNormal;
32 bool BrlCadInterface::m_Hit;
33 double BrlCadInterface::m_InRadius;
34 double BrlCadInterface::m_OutRadius;
36 BrlCadInterface::BrlCadInterface(QString file_name, QString object_name)
38 m_Rtip = rt_dirbuild(qPrintable(file_name), m_IdBuf, sizeof(m_IdBuf));
39 if (m_Rtip == RTI_NULL) {
40 EG_ERR_RETURN("Unable to open BRL-CAD database!");
42 if (rt_gettree(m_Rtip, qPrintable(object_name)) < 0) {
43 EG_ERR_RETURN("unable to access selected object");
45 rt_prep_parallel(m_Rtip, 1);
46 application ap = {0};
47 m_Ap = ap;
48 m_Ap.a_rt_i = m_Rtip;
50 setName("BRL-CAD interface");
51 m_ShootRayImplemented = true;
53 //m_Ap.a_onehit = 1;
56 int BrlCadInterface::hit(application *ap, struct partition *PartHeadp, seg *segs)
58 register struct partition *pp;
59 register struct hit *hitp;
60 register struct soltab *stp;
61 struct curvature cur;
62 point_t pt;
63 vect_t inormal;
64 vect_t onormal;
65 double curv;
66 int N = 0;
67 //for (pp=PartHeadp->pt_forw; pp != PartHeadp; pp = pp->pt_forw) {
68 pp = PartHeadp->pt_forw;
70 ++N;
72 hitp = pp->pt_inhit;
73 stp = pp->pt_inseg->seg_stp;
75 VJOIN1(pt, ap->a_ray.r_pt, hitp->hit_dist, ap->a_ray.r_dir);
77 RT_HIT_NORMAL(inormal, hitp, stp, &(ap->a_ray), pp->pt_inflip);
78 RT_CURVATURE(&cur, hitp, pp->pt_inflip, stp);
79 curv = max(fabs(cur.crv_c1), fabs(cur.crv_c2));
80 m_InRadius = 1.0/max(1e-10, curv);
82 m_XIn[0] = pt[0];
83 m_XIn[1] = pt[1];
84 m_XIn[2] = pt[2];
85 m_InNormal[0] = inormal[0];
86 m_InNormal[1] = inormal[1];
87 m_InNormal[2] = inormal[2];
89 hitp = pp->pt_outhit;
90 stp = pp->pt_outseg->seg_stp;
91 VJOIN1(pt, ap->a_ray.r_pt, hitp->hit_dist, ap->a_ray.r_dir);
92 RT_HIT_NORMAL( onormal, hitp, stp, &(ap->a_ray), pp->pt_outflip );
93 RT_CURVATURE(&cur, hitp, pp->pt_inflip, stp);
94 curv = max(fabs(cur.crv_c1), fabs(cur.crv_c2));
95 m_OutRadius = 1.0/max(1e-10, curv);
97 m_XOut[0] = pt[0];
98 m_XOut[1] = pt[1];
99 m_XOut[2] = pt[2];
100 m_OutNormal[0] = onormal[0];
101 m_OutNormal[1] = onormal[1];
102 m_OutNormal[2] = onormal[2];
105 m_Hit = true;
106 return(0);
109 int BrlCadInterface::miss(application *ap)
111 m_Hit = false;
112 return(0);
115 bool BrlCadInterface::brlCadShootRay(vec3_t x, vec3_t v, vec3_t &x_in, vec3_t &x_out, vec3_t &n_in, vec3_t &n_out, double &r_in, double &r_out)
117 if (!checkVector(x)) {
118 EG_BUG;
120 if (!checkVector(v)) {
121 EG_BUG;
124 VSET(m_Ap.a_ray.r_pt, x[0], x[1], x[2]);
125 VSET(m_Ap.a_ray.r_dir, v[0], v[1], v[2]);
126 m_Hit = false;
127 m_Ap.a_hit = BrlCadInterface::hit;
128 m_Ap.a_miss = BrlCadInterface::miss;
129 rt_shootray(&m_Ap);
130 x_in = m_XIn;
131 n_in = m_InNormal;
132 r_in = m_InRadius;
133 x_out = m_XOut;
134 n_out = m_OutNormal;
135 r_out = m_OutRadius;
136 x = x_out;
137 if (!checkVector(x_in)) {
138 EG_BUG;
140 if (!checkVector(x_out)) {
141 EG_BUG;
143 if (!checkVector(n_in)) {
144 EG_BUG;
146 if (!checkVector(n_out)) {
147 EG_BUG;
149 return m_Hit;
152 BrlCadInterface::HitType BrlCadInterface::shootRay(vec3_t x, vec3_t v, vec3_t &x_hit, vec3_t &n_hit, double &r)
154 HitType hit_type = Miss;
155 v.normalise();
156 vec3_t x_in, x_out, n_in, n_out;
157 double r_in, r_out;
158 if (brlCadShootRay(x, v, x_in, x_out, n_in, n_out, r_in, r_out)) {
159 double d_in = (x_in - x)*v;
160 if (d_in > 0) {
161 x_hit = x_in;
162 n_hit = n_in;
163 r = r_in;
164 hit_type = HitIn;
166 double d_out = (x_out - x)*v;
167 if (d_out > 0) {
168 if (hit_type == Miss || d_out < d_in) {
169 x_hit = x_out;
170 n_hit = n_out;
171 hit_type = HitOut;
172 r = r_out;
176 return hit_type;
180 BrlCadInterface::PositionType BrlCadInterface::position(vec3_t x, vec3_t n)
182 vec3_t x_hit, n_hit;
183 double r_hit;
184 HitType hit_type = shootRay(x, n, x_hit, n_hit, r_hit);
185 if (hit_type == HitOut) {
186 return Inside;
188 if (hit_type == HitIn) {
189 return Outside;
192 // try to shoot in the opposite direction
193 n *= -1;
194 hit_type = shootRay(x, n, x_hit, n_hit, r_hit);
195 if (hit_type == HitOut) {
196 return Inside;
198 if (hit_type == HitIn) {
199 return Outside;
202 return Surface;
206 #endif // BRLCAD_SUPPORT