summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/src/cython/_cy_conicsection.pyx
blob: 91bbd73c3e5624714267aa881bb86282850efa7a (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
from cython.operator cimport dereference as deref
from numbers import Number
from _cy_rectangle cimport cy_OptInterval, wrap_OptInterval, wrap_Rect, OptRect, wrap_OptRect
from _cy_rectangle cimport cy_Interval, wrap_Interval   

from _cy_affine cimport cy_Affine, wrap_Affine, get_Affine, is_transform

from _cy_curves cimport is_Curve, get_Curve_p
from _cy_path cimport wrap_Path


cdef class cy_Circle:
    
    """Circle in 2D plane.
    
    Corresponds to Circle class in 2geom.
    """

    cdef Circle* thisptr

    def __cinit__(self, cy_Point center=None, double r=0):
        """Create circle from center and radius."""
        if center is None:
            self.thisptr = new Circle()
        else:
            self.thisptr = new Circle(deref( center.thisptr ), r)

    @classmethod
    def from_coefficients(self, double A, double B, double C, double D):
        """Create circle form implicit equation coefficients:
        
        Implicit equation is Ax**2 + Ay**2 + Bx + Cy + D = 0
        """
        return wrap_Circle( Circle(A, B, C, D) )

    @classmethod
    def from_points(self, points):
        """Create best fitting circle from at least three points."""
        return wrap_Circle( Circle( make_vector_point(points) ) )

    def set_center(self, cy_Point c):
        """Set coordinates of center."""
        self.thisptr.setCenter(deref(c.thisptr))

    def set_radius(self, double r):
        """Set the circle's radius."""
        self.thisptr.setRadius(r)

    def set_coefficients(self, double A, double B, double C, double D):
        """Set implicit equation coefficients:
        
        Implicit equation is Ax**2 + Ay**2 + Bx + Cy + D = 0
        """
        self.thisptr.set(A, B, C, D)
    
    def fit(self, points):
        """Set circle to the best fit of at least three points."""
        self.thisptr.fit( make_vector_point(points) )
        
    def arc(self, cy_Point initial, cy_Point inner, cy_Point final, bint _svg_compliant=True):
        """Get (SVG)EllipticalArc.
        
        Args:
            initial: Initial point of arc
            inner: Inner point of arc.
            final: Final point of arc.
        """
        return wrap_EllipticalArc( deref(self.thisptr.arc(deref( initial.thisptr ), deref( inner.thisptr ), deref( final.thisptr ))) )

    def center(self):
        """Get center of circle in point."""
        return wrap_Point(self.thisptr.center())

    def radius(self):
        """Get radius of circle."""
        return self.thisptr.radius()

cdef cy_Circle wrap_Circle(Circle p):
    cdef Circle * retp = new Circle()
    retp[0] = p
    cdef cy_Circle r = cy_Circle.__new__(cy_Circle)
    r.thisptr = retp
    return r

cdef class cy_Ellipse:
    """Ellipse in 2D plane.
    
    Corresponds to Ellipse class in 2geom.
    """
    
    cdef Ellipse* thisptr
    
    def __cinit__(self, cy_Point center=None, rx=0, ry=0, double a=0):
        """Create new ellipse:
        
        Args:
            center: Center of ellipse (between foci)
            rx, ry: major and minor semi-axis.
            a: angle of major axis.
        """
        if center is None:
            self.thisptr = new Ellipse()
        else:
            self.thisptr = new Ellipse(center.x, center.y, rx, ry, a)
    
    @classmethod
    def from_coefficients(cls, double A, double B, double C, double D, double E, double F):
        """Create ellipse from coefficients of implicit equation.
        
        Implicit equation has form Ax**2 + Bxy + Cy**2 + Dx + Ey + F = 0
        """
        return wrap_Ellipse(Ellipse(A, B, C, D, E, F))
    
    @classmethod
    def from_circle(cls, cy_Circle c):
        """Create ellipse identical to circle."""
        return wrap_Ellipse(Ellipse(deref( c.thisptr )))
    
    @classmethod
    def from_points(cls, points):
        """Create ellipse fitting at least 5 points."""
        return wrap_Ellipse( Ellipse( make_vector_point(points) ) )
    
    def set(self, cy_Point center, double rx, double ry, double a):
        """Set center, rays and angle of ellipse.
        
        Args:
            center: Center of ellipse.
            rx, ry: Major and minor semi-axis.
            a: angle of major axis.
        self.thisptr.set(center.x, center.y, rx, ry, a)
        """

    def set_coefficients(self, double A, double B, double C, double D, double E, double F):
        """Set coefficients of implicit equation.
        
        Implicit equation has form Ax**2 + Bxy + Cy**2 + Dx + Ey + F = 0
        """
        self.thisptr.set(A, B, C, D, E, F)

    def set_points(self, points):
        """Set ellipse to the best fit of at least five points."""
        self.thisptr.set( make_vector_point(points) )

    def arc(self, cy_Point initial, cy_Point inner, cy_Point final, bint svg_compliant=True):
        """Get (SVG)EllipticalArc.
        
        Args:
            initial: Initial point of arc
            inner: Inner point of arc.
            final: Final point of arc.
        """
        return wrap_EllipticalArc( deref(self.thisptr.arc(deref( initial.thisptr ), deref( inner.thisptr ), deref( final.thisptr ))) )

    def center(self):
        """Get center of ellipse."""
        return wrap_Point(self.thisptr.center())

    def ray(self, Dim2 d):
        """Get major/minor semi-axis."""
        return self.thisptr.ray(d)

    def rot_angle(self):
        """Get angle of major axis."""
        return self.thisptr.rot_angle()

    def implicit_form_coefficients(self):
        """Get coefficients of implicit equation in list."""
        return wrap_vector_double(self.thisptr.implicit_form_coefficients())

    def transformed(self, m):
        """Return transformed ellipse."""
        cdef Affine at
        if is_transform(m):
            at = get_Affine(m)
        return wrap_Ellipse(self.thisptr.transformed(at))
        
cdef cy_Ellipse wrap_Ellipse(Ellipse p):
    cdef Ellipse * retp = new Ellipse()
    retp[0] = p
    cdef cy_Ellipse r = cy_Ellipse.__new__(cy_Ellipse)
    r.thisptr = retp
    return r