2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008-2013 enGits GmbH +
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. +
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. +
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/>. +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24 #include "brlcadinterface.h"
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);
50 setName("BRL-CAD interface");
51 m_ShootRayImplemented
= true;
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
;
67 //for (pp=PartHeadp->pt_forw; pp != PartHeadp; pp = pp->pt_forw) {
68 pp
= PartHeadp
->pt_forw
;
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
);
85 m_InNormal
[0] = inormal
[0];
86 m_InNormal
[1] = inormal
[1];
87 m_InNormal
[2] = inormal
[2];
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
);
100 m_OutNormal
[0] = onormal
[0];
101 m_OutNormal
[1] = onormal
[1];
102 m_OutNormal
[2] = onormal
[2];
109 int BrlCadInterface::miss(application
*ap
)
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
)) {
120 if (!checkVector(v
)) {
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]);
127 m_Ap
.a_hit
= BrlCadInterface::hit
;
128 m_Ap
.a_miss
= BrlCadInterface::miss
;
137 if (!checkVector(x_in
)) {
140 if (!checkVector(x_out
)) {
143 if (!checkVector(n_in
)) {
146 if (!checkVector(n_out
)) {
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
;
156 vec3_t x_in
, x_out
, n_in
, n_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
;
166 double d_out
= (x_out
- x
)*v
;
168 if (hit_type
== Miss
|| d_out
< d_in
) {
180 BrlCadInterface::PositionType BrlCadInterface::position(vec3_t x, vec3_t n)
184 HitType hit_type = shootRay(x, n, x_hit, n_hit, r_hit);
185 if (hit_type == HitOut) {
188 if (hit_type == HitIn) {
192 // try to shoot in the opposite direction
194 hit_type = shootRay(x, n, x_hit, n_hit, r_hit);
195 if (hit_type == HitOut) {
198 if (hit_type == HitIn) {
206 #endif // BRLCAD_SUPPORT