1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + This file is part of enGrid. +
5 // + Copyright 2008-2014 enGits GmbH +
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. +
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. +
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/>. +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 #include "brlcadinterface.h"
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);
48 setName("BRL-CAD interface");
49 m_ShootRayImplemented
= true;
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
;
65 //for (pp=PartHeadp->pt_forw; pp != PartHeadp; pp = pp->pt_forw) {
66 pp
= PartHeadp
->pt_forw
;
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
);
83 m_InNormal
[0] = inormal
[0];
84 m_InNormal
[1] = inormal
[1];
85 m_InNormal
[2] = inormal
[2];
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
);
98 m_OutNormal
[0] = onormal
[0];
99 m_OutNormal
[1] = onormal
[1];
100 m_OutNormal
[2] = onormal
[2];
107 int BrlCadInterface::miss(application
*ap
)
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
)) {
118 if (!checkVector(v
)) {
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]);
125 m_Ap
.a_hit
= BrlCadInterface::hit
;
126 m_Ap
.a_miss
= BrlCadInterface::miss
;
135 if (!checkVector(x_in
)) {
138 if (!checkVector(x_out
)) {
141 if (!checkVector(n_in
)) {
144 if (!checkVector(n_out
)) {
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
;
154 vec3_t x_in
, x_out
, n_in
, n_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
;
164 double d_out
= (x_out
- x
)*v
;
166 if (hit_type
== Miss
|| d_out
< d_in
) {
178 BrlCadInterface::PositionType BrlCadInterface::position(vec3_t x, vec3_t n)
182 HitType hit_type = shootRay(x, n, x_hit, n_hit, r_hit);
183 if (hit_type == HitOut) {
186 if (hit_type == HitIn) {
190 // try to shoot in the opposite direction
192 hit_type = shootRay(x, n, x_hit, n_hit, r_hit);
193 if (hit_type == HitOut) {
196 if (hit_type == HitIn) {
204 #endif // BRLCAD_SUPPORT