File size: 27,617 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# SPDX-License-Identifier: LGPL-2.1-or-later

# ***************************************************************************
# *   Copyright (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net>        *
# *   Copyright (c) 2009, 2010 Ken Cline <cline@frii.com>                   *
# *                                                                         *
# *   This file is part of the FreeCAD CAx development system.              *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   FreeCAD is distributed in the hope that it will be useful,            *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with FreeCAD; if not, write to the Free Software        *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************
"""Provides various functions for general geometrical calculations."""
## @package geometry
# \ingroup draftgeoutils
# \brief Provides various functions for general geometrical calculations.

import math
import lazy_loader.lazy_loader as lz

import FreeCAD as App
import DraftVecUtils

import draftutils.gui_utils as gui_utils

from draftgeoutils.general import geomType, vec

# Delay import of module until first use because it is heavy
Part = lz.LazyLoader("Part", globals(), "Part")

## \addtogroup draftgeoutils
# @{


def findPerpendicular(point, edgeslist, force=None):
    """Find the perpendicular distance between a point and a list of edges.

    If force is specified, only the edge[force] will be considered,
    and it will be considered infinite.

    Returns
    -------
    [vector_from_point_to_closest_edge, edge_index]
        The vector and the index in the list.

    None
        If no perpendicular vector could be found.
    """
    if not isinstance(edgeslist, list):
        try:
            edgeslist = edgeslist.Edges
        except AttributeError:
            print("Doesn't have 'Edges'")
            return None

    if force is None:
        valid = None
        for edge in edgeslist:
            dist = findDistance(point, edge, strict=True)
            if dist:
                if not valid:
                    valid = [dist, edgeslist.index(edge)]
                else:
                    if dist.Length < valid[0].Length:
                        valid = [dist, edgeslist.index(edge)]
        return valid
    else:
        edge = edgeslist[force]
        dist = findDistance(point, edge)
        if dist:
            return [dist, force]
        else:
            return None
        return None


def findDistance(point, edge, strict=False):
    """Return a vector from the point to its closest point on the edge.

    If `strict` is `True`, the vector will be returned
    only if its endpoint lies on the `edge`.
    Edge can also be a list of 2 points.
    """
    if isinstance(point, App.Vector):
        if isinstance(edge, list):
            segment = edge[1].sub(edge[0])
            chord = edge[0].sub(point)
            norm = segment.cross(chord)
            perp = segment.cross(norm)
            dist = DraftVecUtils.project(chord, perp)

            if not dist:
                return None

            newpoint = point.add(dist)

            if dist.Length == 0:
                return None

            if strict:
                s1 = newpoint.sub(edge[0])
                s2 = newpoint.sub(edge[1])
                if s1.Length <= segment.Length and s2.Length <= segment.Length:
                    return dist
                else:
                    return None
            else:
                return dist

        elif geomType(edge) == "Line":
            segment = vec(edge)
            chord = edge.Vertexes[0].Point.sub(point)
            norm = segment.cross(chord)
            perp = segment.cross(norm)
            dist = DraftVecUtils.project(chord, perp)

            if not dist:
                return None

            newpoint = point.add(dist)

            if dist.Length == 0:
                return None

            if strict:
                s1 = newpoint.sub(edge.Vertexes[0].Point)
                s2 = newpoint.sub(edge.Vertexes[-1].Point)
                if s1.Length <= segment.Length and s2.Length <= segment.Length:
                    return dist
                else:
                    return None
            else:
                return dist

        elif geomType(edge) == "Circle":
            ve1 = edge.Vertexes[0].Point
            if len(edge.Vertexes) > 1:
                ve2 = edge.Vertexes[-1].Point
            else:
                ve2 = None
            center = edge.Curve.Center
            segment = center.sub(point)

            if segment.Length == 0:
                return None

            ratio = (segment.Length - edge.Curve.Radius) / segment.Length
            dist = segment.multiply(ratio)
            newpoint = App.Vector.add(point, dist)

            if dist.Length == 0:
                return None

            if strict and ve2:
                # Note 1: DraftVecUtils.angle(App.Vector(1, 1, 0)) => -0.7854
                # Note 2: Angles are in the +pi to -pi range.
                ang1 = DraftVecUtils.angle(ve1.sub(center))
                ang2 = DraftVecUtils.angle(ve2.sub(center))
                angpt = DraftVecUtils.angle(newpoint.sub(center))
                if ang1 >= ang2:  # Arc does not cross the 9 o'clock point.
                    if ang1 >= angpt and angpt >= ang2:
                        return dist
                    else:
                        return None
                elif ang1 >= angpt or angpt >= ang2:
                    return dist
                else:
                    return None
            else:
                return dist

        elif geomType(edge) == "BSplineCurve" or geomType(edge) == "BezierCurve":
            try:
                pr = edge.Curve.parameter(point)
                np = edge.Curve.value(pr)
                dist = np.sub(point)
            except Part.OCCError:
                print("DraftGeomUtils: Unable to get curve parameter " "for point ", point)
                return None
            else:
                return dist
        else:
            print("DraftGeomUtils: Couldn't project point")
            return None
    else:
        print("DraftGeomUtils: Couldn't project point")
        return None


def get_spline_normal(edge, tol=-1):
    """Find the normal of a BSpline edge."""

    if edge.isNull():
        return None

    if is_straight_line(shape, tol):
        return None

    plane = edge.findPlane(tol)
    if plane:
        normal = plane.Axis
        return normal
    else:
        return None


def get_shape_normal(shape):
    """Find the normal of a shape or list of points or colinear edges, if possible."""

    # New function based on get_normal() drafted by @Roy_043
    # in discussion https://forum.freecad.org/viewtopic.php?p=717862#p717862
    #
    # The normal would not be affected  by the 3D view direction and flipped as
    # get_normal would be. Placement of the Shape is taken into account in this
    # new function instead :
    # - Normal of a planar shape is usually bi-directional, this function return
    #   the one 'in the direction' of the shape's placement 'normal' (z-direction).
    #   As reference, shape.findPlane() favour positive axes, get_normal()
    #   favour the 'view direction' (obtained by getViewDirection() ).
    # - Even when the Shape is an edge or colinear edges, its Placement is taken
    #   into account, and this function return one 'in the direction' of the
    #   shape's placement 'normal' (z-direction).
    #   https://forum.freecad.org/viewtopic.php?p=715850#p715850
    # The normal direction of a Draft wire (co-planar) or arc used to depends on
    # the way the shape is constructed i.e. by vertexes in clockwise or
    # anti-clockwise.  This cryptic behaviour no longer matters.

    if shape.isNull():
        return None

    # Roy_043's remarks on the behavior of Shape.findPlane():
    # - https://forum.freecad.org/viewtopic.php?p=713993#p713993
    # Check if the shape is planar
    # If True: check if the outer wire is closed:
    #    if True  :   Return a (plane with) normal based on the direction of the outer wire (CW or CCW).
    #    if False : ? Return a (plane with) normal that points towards positive global axes with a certain priority
    #                 (probably? Z, Y and then X).
    # If False: return None.
    #
    # Further remarks :
    # - Tested Sketch with 2 colinear edges return a Plane with Axis
    # - Tested Sketch with 1 edge return no Plane with no Axis

    shape_rot = shape.Placement.Rotation
    plane = shape.findPlane()

    if plane is None:
        if not is_straight_line(shape):
            return None
        start_edge = shape.Edges[0]
        x_vec = start_edge.tangentAt(
            start_edge.FirstParameter
        )  # Return vector is in global coordinate
        local_x_vec = shape_rot.inverted().multVec(x_vec)  #
        local_rot = App.Rotation(local_x_vec, App.Vector(0, 1, 0), App.Vector(0, 0, 1), "XZY")
        # see https://blog.freecad.org/2023/01/16/the-rotation-api-in-freecad/ for App.Rotation(vecx, vecy, vecz, string)
        # discussion - https://forum.freecad.org/viewtopic.php?p=717738#p717738
        return shape_rot.multiply(local_rot).multVec(App.Vector(0, 0, 1))

    normal = plane.Axis
    shape_normal = shape_rot.multVec(App.Vector(0, 0, 1))
    # Now, check the normal direction of the plane (plane.Axis).
    # The final deduced normal should be 'in the direction' of the z-direction of the shape's placement,
    # if plane.Axis is in the 'opposite' direction of the Z direction of the shape's placement, reverse it.
    if normal.getAngle(shape_normal) > math.pi / 2:
        normal = normal.negative()
    return normal


def get_normal(shape, tol=-1):
    """Find the normal of a shape or list of points, if possible."""

    # for points
    if isinstance(shape, (list, tuple)):
        if len(shape) <= 2:
            return None
        else:
            poly = Part.makePolygon(shape)
            if is_straight_line(poly, tol):
                return None

            plane = poly.findPlane(tol)
            if plane:
                normal = plane.Axis
                return normal
            else:
                return None

    # for shapes
    if shape.isNull():
        return None

    if is_straight_line(shape, tol):
        return None
    else:
        plane = find_plane(shape, tol)
        if plane:
            normal = plane.Axis
        else:
            return None

    # Check the 3D view to flip the normal if the GUI is available
    if App.GuiUp:
        view = gui_utils.get_3d_view()
        if view is not None:
            v_dir = view.getViewDirection()
            if normal.getAngle(v_dir) < 0.78:
                normal = normal.negative()

    return normal


def getRotation(v1, v2=App.Vector(0, 0, 1)):
    """Get the rotation Quaternion between 2 vectors."""
    if (v1.dot(v2) > 0.999999) or (v1.dot(v2) < -0.999999):
        # vectors are opposite
        return None

    axis = v1.cross(v2)
    axis.normalize()
    # angle = math.degrees(math.sqrt(v1.Length^2 * v2.Length^2) + v1.dot(v2))
    angle = math.degrees(DraftVecUtils.angle(v1, v2, axis))
    return App.Rotation(axis, angle)


def is_planar(shape, tol=-1):
    """Return True if the given shape or list of points is planar."""

    # for points
    if isinstance(shape, list):
        if len(shape) <= 3:
            return True
        else:
            poly = Part.makePolygon(shape)
            if is_straight_line(poly, tol):
                return True

            plane = poly.findPlane(tol)
            if plane:
                return True
            else:
                return False

    # for shapes
    if shape.isNull():
        return False

    # because Part.Shape.findPlane return None for Vertex and straight edges
    if shape.ShapeType == "Vertex":
        return True

    if is_straight_line(shape, tol):
        return True

    plane = find_plane(shape, tol)
    if plane:
        return True
    else:
        return False


def is_straight_line(shape, tol=-1):
    """Return True if shape is a straight line.
    function used in other methods because Part.Shape.findPlane assign a
    plane and normal to straight wires creating privileged directions
    and to deal with straight wires with overlapped edges."""

    if shape.isNull():
        return False

    if len(shape.Faces) != 0:
        return False

    if len(shape.Edges) == 0:
        return False

    if len(shape.Edges) >= 1:
        start_edge = shape.Edges[0]
        dir_start_edge = start_edge.tangentAt(start_edge.FirstParameter)
        point_start_edge = start_edge.firstVertex().Point

        # set tolerance
        if tol <= 0:
            err = shape.globalTolerance(tol)
        else:
            err = tol

        for edge in shape.Edges:
            first_point = edge.firstVertex().Point
            last_point = edge.lastVertex().Point
            dir_edge = edge.tangentAt(edge.FirstParameter)
            # check if edge is curve or no parallel to start_edge
            # because sin(x) = x + O(x**3), for small angular deflection it's
            # enough use the cross product of directions (or dot with a normal)
            if (
                abs(edge.Length - first_point.distanceToPoint(last_point)) > err
                # https://forum.freecad.org/viewtopic.php?p=726101#p726101
                # Shape with parallel edges but not colinear used to return True
                # by this function, below line added fixes this bug.
                # Further remark on the below fix :
                # - get_normal() use this function, may had created problems
                #   previously but not reported; on the other hand, this fix
                #   seems have no 'regression' created as get_normal use
                #   plane.Axis (with 3DView check).  See get_shape_normal()
                #   which take into account shape's placement
                # - other functions had been using this also : Keep In View to
                #    see if there is 'regression' :
                #
                # $ grep -rni "is_straight" ./
                # ./draftfunctions/upgrade.py
                # ./draftgeoutils/geometry.py
                # ./draftmake/make_wire.py
                or first_point.distanceToLine(point_start_edge, dir_start_edge) > err  #
                or dir_start_edge.cross(dir_edge).Length > err
            ):
                return False

    return True


def are_coplanar(shape_a, shape_b, tol=-1):
    """Return True if exist a plane containing both shapes."""

    if shape_a.isNull() or shape_b.isNull():
        return False

    if not is_planar(shape_a, tol) or not is_planar(shape_b, tol):
        return False

    if shape_a.isEqual(shape_b):
        return True

    plane_a = find_plane(shape_a, tol)
    plane_b = find_plane(shape_b, tol)

    # set tolerance
    if tol <= 0:
        err = 1e-7
    else:
        err = tol

    if plane_a and plane_b:
        normal_a = plane_a.Axis
        normal_b = plane_b.Axis
        proj = plane_a.projectPoint(plane_b.Position)
        if normal_a.cross(normal_b).Length > err or plane_b.Position.sub(proj).Length > err:
            return False
        else:
            return True

    elif plane_a and not plane_b:
        normal_a = plane_a.Axis
        for vertex in shape_b.Vertexes:
            dir_ver_b = vertex.Point.sub(plane_a.Position).normalize()
            if abs(normal_a.dot(dir_ver_b)) > err:
                proj = plane_a.projectPoint(vertex.Point)
                if vertex.Point.sub(proj).Length > err:
                    return False
        return True

    elif plane_b and not plane_a:
        normal_b = plane_b.Axis
        for vertex in shape_a.Vertexes:
            dir_ver_a = vertex.Point.sub(plane_b.Position).normalize()
            if abs(normal_b.dot(dir_ver_a)) > err:
                proj = plane_b.projectPoint(vertex.Point)
                if vertex.Point.sub(proj).Length > err:
                    return False
        return True
    # not normal_a and not normal_b:
    else:
        points_a = [vertex.Point for vertex in shape_a.Vertexes]
        points_b = [vertex.Point for vertex in shape_b.Vertexes]
        poly = Part.makePolygon(points_a + points_b)
        if is_planar(poly, tol):
            return True
        else:
            return False


def get_spline_surface_normal(shape, tol=-1):
    """Check if shape formed by BSpline surfaces is planar and get normal.
    If shape is not planar return None."""

    if shape.isNull():
        return None

    if len(shape.Faces) == 0:
        return None

    # set tolerance
    if tol <= 0:
        err = shape.globalTolerance(tol)
    else:
        err = tol

    first_surf = shape.Faces[0].Surface

    if not first_surf.isPlanar(tol):
        return None

    # find bounds of first_surf
    u0, u1, v0, v1 = first_surf.bounds()
    u = (u0 + u1) / 2
    v = (v0 + v1) / 2
    first_normal = first_surf.normal(u, v)
    # check if all faces are planar and parallel
    for face in shape.Faces:
        surf = face.Surface
        if not surf.isPlanar(tol):
            return None
        u0, u1, v0, v1 = surf.bounds()
        u = (u0 + u1) / 2
        v = (v0 + v1) / 2
        surf_normal = surf.normal(u, v)
        if first_normal.cross(surf_normal).Length > err:
            return None

    normal = first_normal

    return normal


def find_plane(shape, tol=-1):
    """Find the plane containing the shape if possible.
    Use this function as a workaround due Part.Shape.findPlane
    fail to find plane on BSpline surfaces."""

    if shape.isNull():
        return None

    if shape.ShapeType == "Vertex":
        return None

    if is_straight_line(shape, tol):
        return None

    plane = shape.findPlane(tol)
    if plane:
        return plane
    elif len(shape.Faces) >= 1:
        # in case shape have BSpline surfaces
        normal = get_spline_surface_normal(shape, tol)
        if normal:
            position = shape.CenterOfMass
            return Part.Plane(position, normal)
        else:
            return None
    else:
        return None


def calculatePlacement(shape):
    """Return a placement located in the center of gravity of the shape.

    If the given shape is planar, return a placement located at the center
    of gravity of the shape, and oriented towards the shape's normal.
    Otherwise, it returns a null placement.
    """
    if not is_planar(shape):
        return App.Placement()

    pos = shape.BoundBox.Center
    norm = get_normal(shape)
    # for backward compatibility with previous getNormal implementation
    if norm is None:
        norm = App.Vector(0, 0, 1)
    pla = App.Placement()
    pla.Base = pos
    r = getRotation(norm)

    if r:
        pla.Rotation = r

    return pla


def mirror(point, edge):
    """Find mirror point relative to an edge."""
    normPoint = point.add(findDistance(point, edge, False))

    if normPoint:
        normPoint_point = App.Vector.sub(point, normPoint)
        normPoint_refl = normPoint_point.negative()
        refl = App.Vector.add(normPoint, normPoint_refl)
        return refl
    else:
        return None


def mirror_matrix(mtx, pos, nor):
    """Return a mirrored copy of a matrix.

    Parameters
    ----------
    mtx: Base::Matrix
        Matrix.
    pos: Base::Vector3
        Point on mirror plane.
    nor: Base::Vector3
        Normal of mirror plane.

    Returns
    -------
    Base::Matrix
    """
    # Code by Jolbas:
    # https://forum.freecad.org/viewtopic.php?p=702793#p702793
    mtx_copy = App.Matrix(mtx)
    mtx_copy.move(-pos)
    mtx_copy.scale(-1)
    mtx_copy = App.Rotation(nor, 180) * mtx_copy
    mtx_copy.move(pos)
    return mtx_copy


def uv_vectors_from_face(face, vec_z=App.Vector(0, 0, 1), tol=-1):
    """Return the u and v vectors of a planar face.

    It is up to the calling function to ensure the face is planar.

    If the u vector matches +/-vec_z, or the v vector matches -vec_z, the
    vectors are rotated to ensure the v vector matches +vec_z.

    Parameters
    ----------
    face: Part.Face
        Face.
    vec_z: Base::Vector3, optional
        Defaults to Vector(0, 0, 1).
        Z axis vector used for reference.
        Is replaced by Vector(0, 0, 1) if it matches the +/-normal of the face.
    tol: float, optional
        Defaults to -1.
        Internal tolerance. 1e-7 is used if tol <=0.

    Returns
    -------
    tuple
        U and v vector (Base::Vector3).
    """
    err = 1e-7 if tol <= 0 else tol
    if not vec_z.isEqual(App.Vector(0, 0, 1), err):
        nor = face.normalAt(0, 0)
        if vec_z.isEqual(nor, err) or vec_z.isEqual(nor.negative(), err):
            vec_z = App.Vector(0, 0, 1)
    vec_u, vec_v = face.tangentAt(0, 0)
    if face.Orientation == "Reversed":
        vec_u, vec_v = vec_v, vec_u
    if vec_v.isEqual(vec_z.negative(), err):
        vec_u, vec_v = vec_u.negative(), vec_v.negative()
    elif vec_u.isEqual(vec_z, err):
        vec_u, vec_v = vec_v.negative(), vec_u
    elif vec_u.isEqual(vec_z.negative(), err):
        vec_u, vec_v = vec_v, vec_u.negative()
    return vec_u, vec_v


def placement_from_face(face, vec_z=App.Vector(0, 0, 1), rotated=False, tol=-1):
    """Return a placement from the center of gravity, and the u and v vectors of a planar face.

    It is up to the calling function to ensure the face is planar.

    Parameters
    ----------
    face: Part.Face
        Face.
    vec_z: Base::Vector3, optional
        Defaults to Vector(0, 0, 1).
        Z axis vector used for reference.
        Is replaced by Vector(0, 0, 1) if it matches the +/-normal of the face.
    rotated: bool, optional
        Defaults to `False`.
        If `False` the v vector of the face defines the Y axis of the placement.
        If `True` the -v vector of the face defines the Z axis of the placement
        (used by Arch_Window).
        The u vector defines the X axis in both cases.
    tol: float, optional
        Defaults to -1.
        Internal tolerance. 1e-7 is used if tol <=0.

    Returns
    -------
    Base::Placement

    See also
    --------
    DraftGeomUtils.uv_vectors_from_face
    """
    pt_pos = face.CenterOfGravity
    vec_u, vec_v = uv_vectors_from_face(face, vec_z, tol)
    if rotated:
        return App.Placement(pt_pos, App.Rotation(vec_u, App.Vector(), vec_v.negative(), "XZY"))
    else:
        return App.Placement(pt_pos, App.Rotation(vec_u, vec_v, App.Vector(), "XYZ"))


def placement_from_points(pt_pos, pt_x, pt_y, as_vectors=False, tol=-1):
    """Return a placement from 3 points defining an origin, an X axis and a Y axis.

    If the vectors calculated from the arguments are too short or parallel,
    the returned placement will have a default rotation.

    Parameters
    ----------
    pt_pos: Base::Vector3
        Origin (Base of Placement).
    pt_x: Base::Vector3
        Point on positive X axis. Or X axis vector if as_vectors is `True`.
    pt_y: Base::Vector3
        Point on positive Y axis. Or Y axis vector if as_vectors is `True`.
    as_vectors: bool, optional
        Defaults to `False`.
        If `True` treat pt_x and pt_y as axis vectors.
    tol: float, optional
        Defaults to -1.
        Internal tolerance. 1e-7 is used if tol <=0.

    Returns
    -------
    Base::Placement

    See also
    --------
    DraftGeomUtils.getRotation
    DraftVecUtils.getRotation
    """
    err = 1e-7 if tol <= 0 else tol
    if as_vectors is False:
        vec_u = pt_x - pt_pos
        vec_v = pt_y - pt_pos
    else:
        vec_u = App.Vector(pt_x)
        vec_v = App.Vector(pt_y)

    if vec_u.Length < err or vec_v.Length < err:
        rot = App.Rotation()
    else:
        vec_u.normalize()
        vec_v.normalize()
        if vec_u.isEqual(vec_v, err) or vec_u.isEqual(vec_v.negative(), err):
            rot = App.Rotation()
        else:
            rot = App.Rotation(vec_u, vec_v, App.Vector(), "XYZ")

    return App.Placement(pt_pos, rot)


# Code separated from WorkingPlane.py (offsetToPoint function).
# Note that the return value of this function has the opposite sign.
def distance_to_plane(point, base, normal):
    """Return the signed distance from a plane to a point.

    The distance is positive if the point lies on the +normal side of the plane.

    Parameters
    ----------
    point: Base::Vector3
        Point to project.
    base: Base::Vector3
        Point on plane.
    normal: Base::Vector3
        Normal of plane.

    Returns
    -------
    float
    """
    return (point - base).dot(normal)


# Code separated from WorkingPlane.py (projectPoint function).
# See: https://github.com/FreeCAD/FreeCAD/pull/5307
def project_point_on_plane(point, base, normal, direction=None, force_projection=False, tol=-1):
    """Project a point onto a plane.

    Parameters
    ----------
    point: Base::Vector3
        Point to project.
    base: Base::Vector3
        Point on plane.
    normal: Base::Vector3
        Normal of plane.
    direction: Base::Vector3, optional
        Defaults to `None` in which case the normal is used.
        Direction of projection.
    force_projection: Bool, optional
        Defaults to `False`.
        If `True` forces the projection if the deviation between the direction
        and the normal is less than tol from the orthogonality. The direction
        of projection is then modified to a tol deviation between the direction
        and the orthogonal.
    tol: float, optional
        Defaults to -1.
        Internal tolerance. 1e-7 is used if tol <=0.

    Returns
    -------
    Base::Vector3 or `None`
    """
    err = 1e-7 if tol <= 0 else tol
    normal = App.Vector(normal).normalize()
    if direction is None:
        direction = normal
    else:
        direction = App.Vector(direction).normalize()

    cos = direction.dot(normal)
    delta_ax_proj = (point - base).dot(normal)
    # check the only conflicting case: direction orthogonal to normal
    if abs(cos) < err:
        if force_projection:
            cos = math.copysign(err, delta_ax_proj)
            direction = normal.cross(direction).cross(normal) - cos * normal
        else:
            return None

    return point - delta_ax_proj / cos * direction


# compatibility layer

getSplineNormal = get_spline_normal

getNormal = get_normal

isPlanar = is_planar


## @}