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"
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");
53 int BrlCadInterface::hit(application
*ap
, struct partition
*PartHeadp
, seg
*segs
)
55 register struct partition
*pp
;
56 register struct hit
*hitp
;
57 register struct soltab
*stp
;
64 //for (pp=PartHeadp->pt_forw; pp != PartHeadp; pp = pp->pt_forw) {
65 pp
= PartHeadp
->pt_forw
;
70 stp
= pp
->pt_inseg
->seg_stp
;
72 VJOIN1(pt
, ap
->a_ray
.r_pt
, hitp
->hit_dist
, ap
->a_ray
.r_dir
);
74 RT_HIT_NORMAL(inormal
, hitp
, stp
, &(ap
->a_ray
), pp
->pt_inflip
);
75 RT_CURVATURE(&cur
, hitp
, pp
->pt_inflip
, stp
);
76 curv
= max(fabs(cur
.crv_c1
), fabs(cur
.crv_c2
));
77 m_InRadius
= 1.0/max(1e-10, curv
);
82 m_InNormal
[0] = inormal
[0];
83 m_InNormal
[1] = inormal
[1];
84 m_InNormal
[2] = inormal
[2];
87 stp
= pp
->pt_outseg
->seg_stp
;
88 VJOIN1(pt
, ap
->a_ray
.r_pt
, hitp
->hit_dist
, ap
->a_ray
.r_dir
);
89 RT_HIT_NORMAL( onormal
, hitp
, stp
, &(ap
->a_ray
), pp
->pt_outflip
);
90 RT_CURVATURE(&cur
, hitp
, pp
->pt_inflip
, stp
);
91 curv
= max(fabs(cur
.crv_c1
), fabs(cur
.crv_c2
));
92 m_OutRadius
= 1.0/max(1e-10, curv
);
97 m_OutNormal
[0] = onormal
[0];
98 m_OutNormal
[1] = onormal
[1];
99 m_OutNormal
[2] = onormal
[2];
105 int BrlCadInterface::miss(application
*ap
)
110 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
)
112 if (!checkVector(x
)) {
115 if (!checkVector(v
)) {
119 VSET(m_Ap
.a_ray
.r_pt
, x
[0], x
[1], x
[2]);
120 VSET(m_Ap
.a_ray
.r_dir
, v
[0], v
[1], v
[2]);
122 m_Ap
.a_hit
= BrlCadInterface::hit
;
123 m_Ap
.a_miss
= BrlCadInterface::miss
;
132 if (!checkVector(x_in
)) {
135 if (!checkVector(x_out
)) {
138 if (!checkVector(n_in
)) {
141 if (!checkVector(n_out
)) {
147 BrlCadInterface::HitType
BrlCadInterface::shootRay(vec3_t x
, vec3_t v
, vec3_t
&x_hit
, vec3_t
&n_hit
, double &r
)
149 HitType hit_type
= Miss
;
151 vec3_t x_in
, x_out
, n_in
, n_out
;
153 if (brlCadShootRay(x
, v
, x_in
, x_out
, n_in
, n_out
, r_in
, r_out
)) {
154 double d_in
= (x_in
- x
)*v
;
161 double d_out
= (x_out
- x
)*v
;
163 if (hit_type
== Miss
|| d_out
< d_in
) {
175 BrlCadInterface::PositionType BrlCadInterface::position(vec3_t x, vec3_t n)
179 HitType hit_type = shootRay(x, n, x_hit, n_hit, r_hit);
180 if (hit_type == HitOut) {
183 if (hit_type == HitIn) {
187 // try to shoot in the opposite direction
189 hit_type = shootRay(x, n, x_hit, n_hit, r_hit);
190 if (hit_type == HitOut) {
193 if (hit_type == HitIn) {