fixed edge display for volume cells
[engrid-github.git] / src / libengrid / brlcadinterface.cpp
blob8f2a4aa1ab3b79a69b6a18acda59172573c94713
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
11 // + +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
16 // + +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 #include "brlcadinterface.h"
24 #ifdef BRLCAD_SUPPORT
26 vec3_t BrlCadInterface::m_XIn;
27 vec3_t BrlCadInterface::m_XOut;
28 vec3_t BrlCadInterface::m_InNormal;
29 vec3_t BrlCadInterface::m_OutNormal;
30 bool BrlCadInterface::m_Hit;
31 double BrlCadInterface::m_InRadius;
32 double BrlCadInterface::m_OutRadius;
34 BrlCadInterface::BrlCadInterface(QString file_name, QString object_name)
36 m_Rtip = rt_dirbuild(qPrintable(file_name), m_IdBuf, sizeof(m_IdBuf));
37 if (m_Rtip == RTI_NULL) {
38 EG_ERR_RETURN("Unable to open BRL-CAD database!");
40 if (rt_gettree(m_Rtip, qPrintable(object_name)) < 0) {
41 EG_ERR_RETURN("unable to access selected object");
43 rt_prep_parallel(m_Rtip, 1);
44 application ap = {0};
45 m_Ap = ap;
46 m_Ap.a_rt_i = m_Rtip;
48 setName("BRL-CAD interface");
49 m_ShootRayImplemented = true;
51 //m_Ap.a_onehit = 1;
54 int BrlCadInterface::hit(application *ap, struct partition *PartHeadp, seg *segs)
56 register struct partition *pp;
57 register struct hit *hitp;
58 register struct soltab *stp;
59 struct curvature cur;
60 point_t pt;
61 vect_t inormal;
62 vect_t onormal;
63 double curv;
64 int N = 0;
65 //for (pp=PartHeadp->pt_forw; pp != PartHeadp; pp = pp->pt_forw) {
66 pp = PartHeadp->pt_forw;
68 ++N;
70 hitp = pp->pt_inhit;
71 stp = pp->pt_inseg->seg_stp;
73 VJOIN1(pt, ap->a_ray.r_pt, hitp->hit_dist, ap->a_ray.r_dir);
75 RT_HIT_NORMAL(inormal, hitp, stp, &(ap->a_ray), pp->pt_inflip);
76 RT_CURVATURE(&cur, hitp, pp->pt_inflip, stp);
77 curv = max(fabs(cur.crv_c1), fabs(cur.crv_c2));
78 m_InRadius = 1.0/max(1e-10, curv);
80 m_XIn[0] = pt[0];
81 m_XIn[1] = pt[1];
82 m_XIn[2] = pt[2];
83 m_InNormal[0] = inormal[0];
84 m_InNormal[1] = inormal[1];
85 m_InNormal[2] = inormal[2];
87 hitp = pp->pt_outhit;
88 stp = pp->pt_outseg->seg_stp;
89 VJOIN1(pt, ap->a_ray.r_pt, hitp->hit_dist, ap->a_ray.r_dir);
90 RT_HIT_NORMAL( onormal, hitp, stp, &(ap->a_ray), pp->pt_outflip );
91 RT_CURVATURE(&cur, hitp, pp->pt_inflip, stp);
92 curv = max(fabs(cur.crv_c1), fabs(cur.crv_c2));
93 m_OutRadius = 1.0/max(1e-10, curv);
95 m_XOut[0] = pt[0];
96 m_XOut[1] = pt[1];
97 m_XOut[2] = pt[2];
98 m_OutNormal[0] = onormal[0];
99 m_OutNormal[1] = onormal[1];
100 m_OutNormal[2] = onormal[2];
103 m_Hit = true;
104 return(0);
107 int BrlCadInterface::miss(application *ap)
109 m_Hit = false;
110 return(0);
113 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)
115 if (!checkVector(x)) {
116 EG_BUG;
118 if (!checkVector(v)) {
119 EG_BUG;
122 VSET(m_Ap.a_ray.r_pt, x[0], x[1], x[2]);
123 VSET(m_Ap.a_ray.r_dir, v[0], v[1], v[2]);
124 m_Hit = false;
125 m_Ap.a_hit = BrlCadInterface::hit;
126 m_Ap.a_miss = BrlCadInterface::miss;
127 rt_shootray(&m_Ap);
128 x_in = m_XIn;
129 n_in = m_InNormal;
130 r_in = m_InRadius;
131 x_out = m_XOut;
132 n_out = m_OutNormal;
133 r_out = m_OutRadius;
134 x = x_out;
135 if (!checkVector(x_in)) {
136 EG_BUG;
138 if (!checkVector(x_out)) {
139 EG_BUG;
141 if (!checkVector(n_in)) {
142 EG_BUG;
144 if (!checkVector(n_out)) {
145 EG_BUG;
147 return m_Hit;
150 BrlCadInterface::HitType BrlCadInterface::shootRay(vec3_t x, vec3_t v, vec3_t &x_hit, vec3_t &n_hit, double &r)
152 HitType hit_type = Miss;
153 v.normalise();
154 vec3_t x_in, x_out, n_in, n_out;
155 double r_in, r_out;
156 if (brlCadShootRay(x, v, x_in, x_out, n_in, n_out, r_in, r_out)) {
157 double d_in = (x_in - x)*v;
158 if (d_in > 0) {
159 x_hit = x_in;
160 n_hit = n_in;
161 r = r_in;
162 hit_type = HitIn;
164 double d_out = (x_out - x)*v;
165 if (d_out > 0) {
166 if (hit_type == Miss || d_out < d_in) {
167 x_hit = x_out;
168 n_hit = n_out;
169 hit_type = HitOut;
170 r = r_out;
174 return hit_type;
178 BrlCadInterface::PositionType BrlCadInterface::position(vec3_t x, vec3_t n)
180 vec3_t x_hit, n_hit;
181 double r_hit;
182 HitType hit_type = shootRay(x, n, x_hit, n_hit, r_hit);
183 if (hit_type == HitOut) {
184 return Inside;
186 if (hit_type == HitIn) {
187 return Outside;
190 // try to shoot in the opposite direction
191 n *= -1;
192 hit_type = shootRay(x, n, x_hit, n_hit, r_hit);
193 if (hit_type == HitOut) {
194 return Inside;
196 if (hit_type == HitIn) {
197 return Outside;
200 return Surface;
204 #endif // BRLCAD_SUPPORT