Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DT_BBoxTree.h « complex « src « solid « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3d9da6e34ba303706bc9cf76522ed82bce4240cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
 * SOLID - Software Library for Interference Detection
 * 
 * Copyright (C) 2001-2003  Dtecta.  All rights reserved.
 *
 * This library may be distributed under the terms of the Q Public License
 * (QPL) as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * This library may be distributed and/or modified under the terms of the
 * GNU General Public License (GPL) version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * This library is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Commercial use or any other use of this library not covered by either 
 * the QPL or the GPL requires an additional license from Dtecta. 
 * Please contact info@dtecta.com for enquiries about the terms of commercial
 * use of this library.
 */

#ifndef DT_BBOXTREE_H
#define DT_BBOXTREE_H

#include <new>
#include <algorithm>

#include "DT_Convex.h"
#include "DT_CBox.h"


class DT_BBoxTree {
public:
    enum NodeType { INTERNAL = 0, LEAF = 1 };
    
    DT_BBoxTree() {}
    DT_BBoxTree(const DT_CBox& cbox, DT_Index index, NodeType type) 
      : m_cbox(cbox),
        m_index(index),
        m_type(type)
    {}
    
    DT_CBox  m_cbox;
    DT_Index m_index;
    NodeType m_type;
};



class DT_BBoxNode {
public:
    DT_BBoxNode() {}    
    DT_BBoxNode(int first, int last, int& node, DT_BBoxNode *free_nodes, const DT_CBox *boxes, DT_Index *indices, const DT_CBox& bbox);

    void makeChildren(DT_BBoxTree& ltree, DT_BBoxTree& rtree) const;
    void makeChildren(const DT_CBox& added, DT_BBoxTree& ltree, DT_BBoxTree& rtree) const;

    DT_CBox hull() const { return m_lbox.hull(m_rbox); }  
    
    enum FlagType { LLEAF = 0x80, RLEAF = 0x40 };

    DT_CBox              m_lbox;
    DT_CBox              m_rbox;
    DT_Index             m_lchild;
    DT_Index             m_rchild;
    unsigned char        m_flags;
};

inline void DT_BBoxNode::makeChildren(DT_BBoxTree& ltree, DT_BBoxTree& rtree) const
{
    new (&ltree) DT_BBoxTree(m_lbox, m_lchild, (m_flags & LLEAF) ? DT_BBoxTree::LEAF : DT_BBoxTree::INTERNAL);
    new (&rtree) DT_BBoxTree(m_rbox, m_rchild, (m_flags & RLEAF) ? DT_BBoxTree::LEAF : DT_BBoxTree::INTERNAL);

}

inline void DT_BBoxNode::makeChildren(const DT_CBox& added, DT_BBoxTree& ltree, DT_BBoxTree& rtree) const
{ 
    new (&ltree) DT_BBoxTree(m_lbox + added, m_lchild, (m_flags & LLEAF) ? DT_BBoxTree::LEAF : DT_BBoxTree::INTERNAL);
    new (&rtree) DT_BBoxTree(m_rbox + added, m_rchild, (m_flags & RLEAF) ? DT_BBoxTree::LEAF : DT_BBoxTree::INTERNAL);
}


template <typename Shape>
class DT_RootData {
public:
    DT_RootData(const DT_BBoxNode *nodes, 
                const Shape *leaves) 
      : m_nodes(nodes),
        m_leaves(leaves)
    {}

    const DT_BBoxNode   *m_nodes;
    const Shape         *m_leaves;
};

template <typename Shape1, typename Shape2>
class DT_ObjectData : public DT_RootData<Shape1> {
public:
    DT_ObjectData(const DT_BBoxNode *nodes, 
                  const Shape1 *leaves, 
                  const MT_Transform& xform, 
                  Shape2 plus) 
      : DT_RootData<Shape1>(nodes, leaves),
        m_xform(xform),
        m_inv_xform(xform.inverse()),   
        m_plus(plus),
        m_added(computeCBox(plus, m_inv_xform))
    {}

    const MT_Transform&  m_xform;
    MT_Transform         m_inv_xform;
    Shape2               m_plus;
    DT_CBox              m_added;
};

template <typename Shape1, typename Shape2>
class DT_Pack {
public:
    DT_Pack(const DT_ObjectData<Shape1, Shape2>& a, const DT_Convex& b)
      : m_a(a),
        m_b(b),
        m_b_cbox(b.bbox(m_a.m_inv_xform))
    {}
    
    DT_ObjectData<Shape1, Shape2>  m_a;
    const DT_Convex&               m_b;
    DT_CBox                        m_b_cbox;
};

template <typename Shape1, typename Shape2>
class DT_HybridPack : public DT_Pack<Shape1, Shape2> {
public:
    DT_HybridPack(const DT_ObjectData<Shape1, Shape2>& a, const DT_Convex& b, MT_Scalar margin)
      : DT_Pack<Shape1, Shape2>(a, b),
        m_margin(margin)
    {
        this->m_b_cbox += computeCBox(margin, this->m_a.m_inv_xform);
    }
    
    MT_Scalar m_margin;
};

template <typename Shape1, typename Shape2>
class DT_DuoPack {
public:
    DT_DuoPack(const DT_ObjectData<Shape1, Shape2>& a, const DT_ObjectData<Shape1, Shape2>& b) 
      : m_a(a),
        m_b(b)
    {
        m_b2a = a.m_inv_xform * b.m_xform;
        m_a2b = b.m_inv_xform * a.m_xform;
        m_abs_b2a = m_b2a.getBasis().absolute();
        m_abs_a2b = m_a2b.getBasis().absolute();    
    }
    
    DT_ObjectData<Shape1, Shape2>  m_a, m_b;
    MT_Transform                   m_b2a, m_a2b;
    MT_Matrix3x3                   m_abs_b2a, m_abs_a2b;
};


template <typename Shape>
inline void refit(DT_BBoxNode& node, const DT_RootData<Shape>& rd)
{
    node.m_lbox = (node.m_flags & DT_BBoxNode::LLEAF) ? 
                  computeCBox(rd.m_leaves[node.m_lchild]) : 
                  rd.m_nodes[node.m_lchild].hull(); 
    node.m_rbox = (node.m_flags & DT_BBoxNode::RLEAF) ? 
                  computeCBox(rd.m_leaves[node.m_rchild]) : 
                  rd.m_nodes[node.m_rchild].hull(); 
}


template <typename Shape>
bool ray_cast(const DT_BBoxTree& a, const DT_RootData<Shape>& rd,
              const MT_Point3& source, const MT_Point3& target, 
              MT_Scalar& lambda, MT_Vector3& normal) 
{
    if (!a.m_cbox.overlapsLineSegment(source, source.lerp(target, lambda))) 
    {
        return false;
    }

    if (a.m_type == DT_BBoxTree::LEAF) 
    { 
        return ray_cast(rd, a.m_index, source, target, lambda, normal); 
    }
    else 
    {
        DT_BBoxTree ltree, rtree;
        rd.m_nodes[a.m_index].makeChildren(ltree, rtree);
        
        bool lresult = ray_cast(ltree, rd, source, target, lambda, normal);
        bool rresult = ray_cast(rtree, rd, source, target, lambda, normal);
        return lresult || rresult;
    }
}


#ifdef STATISTICS
int num_box_tests = 0;
#endif

template <typename Shape1, typename Shape2>
inline bool intersect(const DT_CBox& a, const DT_CBox& b, const DT_DuoPack<Shape1, Shape2>& pack)
{
#ifdef STATISTICS
    ++num_box_tests;
#endif

    
    MT_Vector3 abs_pos_b2a = (pack.m_b2a(b.getCenter()) - a.getCenter()).absolute();
    MT_Vector3 abs_pos_a2b = (pack.m_a2b(a.getCenter()) - b.getCenter()).absolute();
    return  (a.getExtent()[0] + pack.m_abs_b2a[0].dot(b.getExtent()) >=  abs_pos_b2a[0]) && 
            (a.getExtent()[1] + pack.m_abs_b2a[1].dot(b.getExtent()) >=  abs_pos_b2a[1]) && 
            (a.getExtent()[2] + pack.m_abs_b2a[2].dot(b.getExtent()) >=  abs_pos_b2a[2]) && 
            (b.getExtent()[0] + pack.m_abs_a2b[0].dot(a.getExtent()) >=  abs_pos_a2b[0]) && 
            (b.getExtent()[1] + pack.m_abs_a2b[1].dot(a.getExtent()) >=  abs_pos_a2b[1]) &&
            (b.getExtent()[2] + pack.m_abs_a2b[2].dot(a.getExtent()) >=  abs_pos_a2b[2]);
}




template <typename Shape1, typename Shape2>
bool intersect(const DT_BBoxTree& a, const DT_Pack<Shape1, Shape2>& pack, MT_Vector3& v)
{ 
    if (!a.m_cbox.overlaps(pack.m_b_cbox)) 
    {
        return false;
    }

    if (a.m_type == DT_BBoxTree::LEAF) 
    {
        return intersect(pack, a.m_index, v);
    }
    else 
    {
        DT_BBoxTree a_ltree, a_rtree;
        pack.m_a.m_nodes[a.m_index].makeChildren(pack.m_a.m_added, a_ltree, a_rtree);
        return intersect(a_ltree, pack, v) || intersect(a_rtree, pack, v);
    }
}

template <typename Shape1, typename Shape2>
bool intersect(const DT_BBoxTree& a, const DT_BBoxTree& b, const DT_DuoPack<Shape1, Shape2>& pack, MT_Vector3& v)
{ 
    if (!intersect(a.m_cbox, b.m_cbox, pack)) 
    {
        return false;
    }

    if (a.m_type == DT_BBoxTree::LEAF && b.m_type == DT_BBoxTree::LEAF) 
    {
        return intersect(pack, a.m_index, b.m_index, v);
    }
    else if (a.m_type == DT_BBoxTree::LEAF || 
             (b.m_type != DT_BBoxTree::LEAF && a.m_cbox.size() < b.m_cbox.size())) 
    {
        DT_BBoxTree b_ltree, b_rtree;
        pack.m_b.m_nodes[b.m_index].makeChildren(pack.m_b.m_added, b_ltree, b_rtree);

        return intersect(a, b_ltree, pack, v) || intersect(a, b_rtree, pack, v);
    }
    else 
    {
        DT_BBoxTree a_ltree, a_rtree;
        pack.m_a.m_nodes[a.m_index].makeChildren(pack.m_a.m_added, a_ltree, a_rtree);
        return intersect(a_ltree, b, pack, v) || intersect(a_rtree, b, pack, v);
    }
}

template <typename Shape1, typename Shape2>
bool common_point(const DT_BBoxTree& a, const DT_Pack<Shape1, Shape2>& pack,  
                  MT_Vector3& v, MT_Point3& pa, MT_Point3& pb)
{ 
    if (!a.m_cbox.overlaps(pack.m_b_cbox))
    {
        return false;
    }

    if (a.m_type == DT_BBoxTree::LEAF) 
    {
        return common_point(pack, a.m_index, v, pa, pb);
    }
    else 
    {
        DT_BBoxTree a_ltree, a_rtree;
        pack.m_a.m_nodes[a.m_index].makeChildren(pack.m_a.m_added, a_ltree, a_rtree);
        return common_point(a_ltree, pack, v, pa, pb) ||
               common_point(a_rtree, pack, v, pa ,pb);
    }
}

template <typename Shape1, typename Shape2>
bool common_point(const DT_BBoxTree& a, const DT_BBoxTree& b, const DT_DuoPack<Shape1, Shape2>& pack,  
                  MT_Vector3& v, MT_Point3& pa, MT_Point3& pb)
{ 
    if (!intersect(a.m_cbox, b.m_cbox, pack))
    {
        return false;
    }

    if (a.m_type == DT_BBoxTree::LEAF && b.m_type == DT_BBoxTree::LEAF) 
    {
        return common_point(pack, a.m_index, b.m_index, v, pa, pb);
    }
    else if (a.m_type == DT_BBoxTree::LEAF || 
             (b.m_type != DT_BBoxTree::LEAF && a.m_cbox.size() < b.m_cbox.size())) 
    {
        DT_BBoxTree b_ltree, b_rtree;
        pack.m_b.m_nodes[b.m_index].makeChildren(pack.m_b.m_added, b_ltree, b_rtree);
        return common_point(a, b_ltree, pack, v, pa, pb) ||
               common_point(a, b_rtree, pack, v, pa, pb);
    }
    else 
    {
        DT_BBoxTree a_ltree, a_rtree;
        pack.m_a.m_nodes[a.m_index].makeChildren(pack.m_a.m_added, a_ltree, a_rtree);
        return common_point(a_ltree, b, pack, v, pa, pb) ||
               common_point(a_rtree, b, pack, v, pa ,pb);
    }
}


template <typename Shape1, typename Shape2>
bool penetration_depth(const DT_BBoxTree& a, const DT_HybridPack<Shape1, Shape2>& pack, 
                       MT_Vector3& v, MT_Point3& pa, MT_Point3& pb, MT_Scalar& max_pen_len) 
{ 
    if (!a.m_cbox.overlaps(pack.m_b_cbox))
    {
        return false;
    }
    
    if (a.m_type == DT_BBoxTree::LEAF) 
    {
        if (penetration_depth(pack, a.m_index, v, pa, pb))
        {
            max_pen_len = pa.distance2(pb);
            return true;
        }
        else 
        {
            return false;
        }
    }
    else 
    {
        DT_BBoxTree a_ltree, a_rtree;
        pack.m_a.m_nodes[a.m_index].makeChildren(pack.m_a.m_added, a_ltree, a_rtree);
        if (penetration_depth(a_ltree, pack, v, pa, pb, max_pen_len)) 
        {
            MT_Vector3 rv;
            MT_Point3 rpa, rpb;
            MT_Scalar rmax_pen_len;
            if (penetration_depth(a_rtree, pack, rv, rpa, rpb, rmax_pen_len) &&
                (max_pen_len < rmax_pen_len))
            {
                max_pen_len = rmax_pen_len;
                v = rv;
                pa = rpa;
                pb = rpb;
            }
            return true;
        }
        else 
        {
            return penetration_depth(a_rtree, pack, v, pa, pb, max_pen_len);
        }
    }
}

template <typename Shape1, typename Shape2>
bool penetration_depth(const DT_BBoxTree& a, const DT_BBoxTree& b, const DT_DuoPack<Shape1, Shape2>& pack, 
                       MT_Vector3& v, MT_Point3& pa, MT_Point3& pb, MT_Scalar& max_pen_len) 
{ 
    if (!intersect(a.m_cbox, b.m_cbox, pack))
    {
        return false;
    }
  
    if (a.m_type == DT_BBoxTree::LEAF && b.m_type == DT_BBoxTree::LEAF) 
    {
        if (penetration_depth(pack, a.m_index, b.m_index, v, pa, pb))
        {
            max_pen_len = pa.distance2(pb);
            return true;
        }
        else 
        {
            return false;
        }
    }
    else if (a.m_type == DT_BBoxTree::LEAF || 
             (b.m_type != DT_BBoxTree::LEAF && a.m_cbox.size() < b.m_cbox.size())) 
    {
        DT_BBoxTree b_ltree, b_rtree;
        pack.m_b.m_nodes[b.m_index].makeChildren(pack.m_b.m_added, b_ltree, b_rtree);
        if (penetration_depth(a, b_ltree, pack, v, pa, pb, max_pen_len)) 
        {
            MT_Point3 rpa, rpb;
            MT_Scalar rmax_pen_len;
            if (penetration_depth(a, b_rtree, pack, v, rpa, rpb, rmax_pen_len) &&
                (max_pen_len < rmax_pen_len))
            {
                max_pen_len = rmax_pen_len;
                pa = rpa;
                pb = rpb;
            }
            return true;
        }
        else
        {
            return penetration_depth(a, b_rtree, pack, v, pa, pb, max_pen_len);
        }
    }
    else 
    {
        DT_BBoxTree a_ltree, a_rtree;
        pack.m_a.m_nodes[a.m_index].makeChildren(pack.m_a.m_added, a_ltree, a_rtree);
        if (penetration_depth(a_ltree, b, pack, v, pa, pb, max_pen_len)) 
        {
            MT_Point3 rpa, rpb;
            MT_Scalar rmax_pen_len;
            if (penetration_depth(a_rtree, b, pack, v, rpa, rpb, rmax_pen_len) &&
                (max_pen_len < rmax_pen_len))
            {
                max_pen_len = rmax_pen_len;
                pa = rpa;
                pb = rpb;
            }
            return true;
        }
        else 
        {
            return penetration_depth(a_rtree, b, pack, v, pa, pb, max_pen_len);
        }
    }
}


// Returns a lower bound for the distance for quick rejection in closest_points
inline MT_Scalar distance2(const DT_CBox& a, const MT_Transform& a2w,
                           const DT_CBox& b, const MT_Transform& b2w)
{
    MT_Vector3 v = b2w(b.getCenter()) - a2w(a.getCenter());
    MT_Scalar dist2 = v.length2();
    if (dist2 > MT_Scalar(0.0))
    {
        MT_Vector3 w = b2w(b.support(-v * b2w.getBasis())) - a2w(a.support(v * a2w.getBasis()));
        MT_Scalar delta = v.dot(w);
        return delta > MT_Scalar(0.0) ? delta * delta / dist2 : MT_Scalar(0.0);
    }
    return MT_Scalar(0.0);
}


template <typename Shape1, typename Shape2>
MT_Scalar closest_points(const DT_BBoxTree& a, const DT_Pack<Shape1, Shape2>& pack, 
                         MT_Scalar max_dist2, MT_Point3& pa, MT_Point3& pb) 
{ 
    if (a.m_type == DT_BBoxTree::LEAF) 
    {
        return closest_points(pack, a.m_index, max_dist2, pa, pb);
    }
    else 
    {
        DT_BBoxTree a_ltree, a_rtree;
        pack.m_a.m_nodes[a.m_index].makeChildren(pack.m_a.m_added, a_ltree, a_rtree);
        MT_Scalar ldist2 = distance2(a_ltree.m_cbox, pack.m_a.m_xform, pack.m_b_cbox, pack.m_a.m_xform);
        MT_Scalar rdist2 = distance2(a_rtree.m_cbox, pack.m_a.m_xform, pack.m_b_cbox, pack.m_a.m_xform);
        if (ldist2 < rdist2) 
        {
            MT_Scalar dist2 = ldist2 < max_dist2 ? closest_points(a_ltree, pack, max_dist2, pa, pb) : MT_INFINITY;
            GEN_set_min(max_dist2, dist2);
            return rdist2 < max_dist2 ? GEN_min(dist2, closest_points(a_rtree, pack, max_dist2, pa, pb)) : dist2;
        }
        else
        {
            MT_Scalar dist2 = rdist2 < max_dist2 ? closest_points(a_rtree, pack, max_dist2, pa, pb) : MT_INFINITY;
            GEN_set_min(max_dist2, dist2);  
            return ldist2 < max_dist2 ? GEN_min(dist2, closest_points(a_ltree, pack, max_dist2, pa, pb)) : dist2;       
        }
    }
}

    
template <typename Shape1, typename Shape2>
MT_Scalar closest_points(const DT_BBoxTree& a, const DT_BBoxTree& b, const DT_DuoPack<Shape1, Shape2>& pack, 
                         MT_Scalar max_dist2, MT_Point3& pa, MT_Point3& pb) 
{   
    if (a.m_type == DT_BBoxTree::LEAF && b.m_type == DT_BBoxTree::LEAF) 
    {
        return closest_points(pack, a.m_index, b.m_index, max_dist2, pa, pb);
    }
    else if (a.m_type == DT_BBoxTree::LEAF || 
             (b.m_type != DT_BBoxTree::LEAF && a.m_cbox.size() < b.m_cbox.size())) 
    {
        DT_BBoxTree b_ltree, b_rtree;
        pack.m_b.m_nodes[b.m_index].makeChildren(pack.m_b.m_added, b_ltree, b_rtree);
        MT_Scalar ldist2 = distance2(a.m_cbox, pack.m_a.m_xform, b_ltree.m_cbox, pack.m_b.m_xform);
        MT_Scalar rdist2 = distance2(a.m_cbox, pack.m_a.m_xform, b_rtree.m_cbox, pack.m_b.m_xform);
        if (ldist2 < rdist2)
        {
            MT_Scalar dist2 = ldist2 < max_dist2 ? closest_points(a, b_ltree, pack, max_dist2, pa, pb): MT_INFINITY;;
            GEN_set_min(max_dist2, dist2);
            return rdist2 < max_dist2 ? GEN_min(dist2, closest_points(a, b_rtree, pack, max_dist2, pa, pb)) : dist2;        
        }
        else
        {
            MT_Scalar dist2 =  rdist2 < max_dist2 ? closest_points(a, b_rtree, pack, max_dist2, pa, pb) : MT_INFINITY;;
            GEN_set_min(max_dist2, dist2);
            return ldist2 < max_dist2 ? GEN_min(dist2, closest_points(a, b_ltree, pack, max_dist2, pa, pb)) : dist2;
        }
    }
    else
    {
        DT_BBoxTree a_ltree, a_rtree;
        pack.m_a.m_nodes[a.m_index].makeChildren(pack.m_a.m_added, a_ltree, a_rtree);
        MT_Scalar ldist2 = distance2(a_ltree.m_cbox, pack.m_a.m_xform, b.m_cbox, pack.m_b.m_xform);
        MT_Scalar rdist2 = distance2(a_rtree.m_cbox, pack.m_a.m_xform, b.m_cbox, pack.m_b.m_xform);
        if (ldist2 < rdist2) 
        {
            MT_Scalar dist2 = ldist2 < max_dist2 ? closest_points(a_ltree, b, pack, max_dist2, pa, pb) : MT_INFINITY;;
            GEN_set_min(max_dist2, dist2);
            return rdist2 < max_dist2 ? GEN_min(dist2,closest_points(a_rtree, b, pack, max_dist2, pa, pb)) : dist2;
        }
        else
        {
            MT_Scalar dist2 = rdist2 < max_dist2 ? closest_points(a_rtree, b, pack, max_dist2, pa, pb) : MT_INFINITY;
            GEN_set_min(max_dist2, dist2);
            return ldist2 < max_dist2 ? GEN_min(dist2, closest_points(a_ltree, b, pack, max_dist2, pa, pb)) : dist2;
        }
    }
}

#endif