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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::Box;
use crate::Plane;
use crate::Point3D;
use crate::Vec2;
use crate::Vec3;
use glib::translate::*;

glib::wrapper! {
    /// A triangle.
    #[derive(Debug, PartialOrd, Ord, Hash)]
    pub struct Triangle(Boxed<ffi::graphene_triangle_t>);

    match fn {
        copy => |ptr| glib::gobject_ffi::g_boxed_copy(ffi::graphene_triangle_get_type(), ptr as *mut _) as *mut ffi::graphene_triangle_t,
        free => |ptr| glib::gobject_ffi::g_boxed_free(ffi::graphene_triangle_get_type(), ptr as *mut _),
        init => |_ptr| (),
        clear => |_ptr| (),
        type_ => || ffi::graphene_triangle_get_type(),
    }
}

impl Triangle {
    /// Checks whether the given triangle `self` contains the point `p`.
    /// ## `p`
    /// a [`Point3D`][crate::Point3D]
    ///
    /// # Returns
    ///
    /// `true` if the point is inside the triangle
    #[doc(alias = "graphene_triangle_contains_point")]
    pub fn contains_point(&self, p: &Point3D) -> bool {
        unsafe {
            from_glib(ffi::graphene_triangle_contains_point(
                self.to_glib_none().0,
                p.to_glib_none().0,
            ))
        }
    }

    /// Checks whether the two given [`Triangle`][crate::Triangle] are equal.
    /// ## `b`
    /// a [`Triangle`][crate::Triangle]
    ///
    /// # Returns
    ///
    /// `true` if the triangles are equal
    #[doc(alias = "graphene_triangle_equal")]
    fn equal(&self, b: &Triangle) -> bool {
        unsafe {
            from_glib(ffi::graphene_triangle_equal(
                self.to_glib_none().0,
                b.to_glib_none().0,
            ))
        }
    }

    /// Computes the area of the given [`Triangle`][crate::Triangle].
    ///
    /// # Returns
    ///
    /// the area of the triangle
    #[doc(alias = "graphene_triangle_get_area")]
    #[doc(alias = "get_area")]
    pub fn area(&self) -> f32 {
        unsafe { ffi::graphene_triangle_get_area(self.to_glib_none().0) }
    }

    /// Computes the [barycentric coordinates](http://en.wikipedia.org/wiki/Barycentric_coordinate_system)
    /// of the given point `p`.
    ///
    /// The point `p` must lie on the same plane as the triangle `self`; if the
    /// point is not coplanar, the result of this function is undefined.
    ///
    /// If we place the origin in the coordinates of the triangle's A point,
    /// the barycentric coordinates are `u`, which is on the AC vector; and `v`
    /// which is on the AB vector:
    ///
    /// ![](triangle-barycentric.png)
    ///
    /// The returned [`Vec2`][crate::Vec2] contains the following values, in order:
    ///
    ///  - `res.x = u`
    ///  - `res.y = v`
    /// ## `p`
    /// a [`Point3D`][crate::Point3D]
    ///
    /// # Returns
    ///
    /// `true` if the barycentric coordinates are valid
    ///
    /// ## `res`
    /// return location for the vector
    ///  with the barycentric coordinates
    #[doc(alias = "graphene_triangle_get_barycoords")]
    #[doc(alias = "get_barycoords")]
    pub fn barycoords(&self, p: Option<&Point3D>) -> Option<Vec2> {
        unsafe {
            let mut res = Vec2::uninitialized();
            let ret = from_glib(ffi::graphene_triangle_get_barycoords(
                self.to_glib_none().0,
                p.to_glib_none().0,
                res.to_glib_none_mut().0,
            ));
            if ret {
                Some(res)
            } else {
                None
            }
        }
    }

    /// Computes the bounding box of the given [`Triangle`][crate::Triangle].
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for the box
    #[doc(alias = "graphene_triangle_get_bounding_box")]
    #[doc(alias = "get_bounding_box")]
    pub fn bounding_box(&self) -> Box {
        unsafe {
            let mut res = Box::uninitialized();
            ffi::graphene_triangle_get_bounding_box(
                self.to_glib_none().0,
                res.to_glib_none_mut().0,
            );
            res
        }
    }

    /// Computes the coordinates of the midpoint of the given [`Triangle`][crate::Triangle].
    ///
    /// The midpoint G is the [centroid](https://en.wikipedia.org/wiki/Centroid`Triangle_centroid`)
    /// of the triangle, i.e. the intersection of its medians.
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for the coordinates of
    ///  the midpoint
    #[doc(alias = "graphene_triangle_get_midpoint")]
    #[doc(alias = "get_midpoint")]
    pub fn midpoint(&self) -> Point3D {
        unsafe {
            let mut res = Point3D::uninitialized();
            ffi::graphene_triangle_get_midpoint(self.to_glib_none().0, res.to_glib_none_mut().0);
            res
        }
    }

    /// Computes the normal vector of the given [`Triangle`][crate::Triangle].
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for the normal vector
    #[doc(alias = "graphene_triangle_get_normal")]
    #[doc(alias = "get_normal")]
    pub fn normal(&self) -> Vec3 {
        unsafe {
            let mut res = Vec3::uninitialized();
            ffi::graphene_triangle_get_normal(self.to_glib_none().0, res.to_glib_none_mut().0);
            res
        }
    }

    /// Computes the plane based on the vertices of the given [`Triangle`][crate::Triangle].
    ///
    /// # Returns
    ///
    ///
    /// ## `res`
    /// return location for the plane
    #[doc(alias = "graphene_triangle_get_plane")]
    #[doc(alias = "get_plane")]
    pub fn plane(&self) -> Plane {
        unsafe {
            let mut res = Plane::uninitialized();
            ffi::graphene_triangle_get_plane(self.to_glib_none().0, res.to_glib_none_mut().0);
            res
        }
    }

    /// Retrieves the three vertices of the given [`Triangle`][crate::Triangle] and returns
    /// their coordinates as [`Point3D`][crate::Point3D].
    ///
    /// # Returns
    ///
    ///
    /// ## `a`
    /// return location for the coordinates
    ///  of the first vertex
    ///
    /// ## `b`
    /// return location for the coordinates
    ///  of the second vertex
    ///
    /// ## `c`
    /// return location for the coordinates
    ///  of the third vertex
    #[doc(alias = "graphene_triangle_get_points")]
    #[doc(alias = "get_points")]
    pub fn points(&self) -> (Point3D, Point3D, Point3D) {
        unsafe {
            let mut a = Point3D::uninitialized();
            let mut b = Point3D::uninitialized();
            let mut c = Point3D::uninitialized();
            ffi::graphene_triangle_get_points(
                self.to_glib_none().0,
                a.to_glib_none_mut().0,
                b.to_glib_none_mut().0,
                c.to_glib_none_mut().0,
            );
            (a, b, c)
        }
    }

    /// Computes the UV coordinates of the given point `p`.
    ///
    /// The point `p` must lie on the same plane as the triangle `self`; if the point
    /// is not coplanar, the result of this function is undefined. If `p` is [`None`],
    /// the point will be set in (0, 0, 0).
    ///
    /// The UV coordinates will be placed in the `res` vector:
    ///
    ///  - `res.x = u`
    ///  - `res.y = v`
    ///
    /// See also: [`barycoords()`][Self::barycoords()]
    /// ## `p`
    /// a [`Point3D`][crate::Point3D]
    /// ## `uv_a`
    /// the UV coordinates of the first point
    /// ## `uv_b`
    /// the UV coordinates of the second point
    /// ## `uv_c`
    /// the UV coordinates of the third point
    ///
    /// # Returns
    ///
    /// `true` if the coordinates are valid
    ///
    /// ## `res`
    /// a vector containing the UV coordinates
    ///  of the given point `p`
    #[doc(alias = "graphene_triangle_get_uv")]
    #[doc(alias = "get_uv")]
    pub fn uv(&self, p: Option<&Point3D>, uv_a: &Vec2, uv_b: &Vec2, uv_c: &Vec2) -> Option<Vec2> {
        unsafe {
            let mut res = Vec2::uninitialized();
            let ret = from_glib(ffi::graphene_triangle_get_uv(
                self.to_glib_none().0,
                p.to_glib_none().0,
                uv_a.to_glib_none().0,
                uv_b.to_glib_none().0,
                uv_c.to_glib_none().0,
                res.to_glib_none_mut().0,
            ));
            if ret {
                Some(res)
            } else {
                None
            }
        }
    }

    /// Retrieves the three vertices of the given [`Triangle`][crate::Triangle].
    ///
    /// # Returns
    ///
    ///
    /// ## `a`
    /// return location for the first vertex
    ///
    /// ## `b`
    /// return location for the second vertex
    ///
    /// ## `c`
    /// return location for the third vertex
    #[doc(alias = "graphene_triangle_get_vertices")]
    #[doc(alias = "get_vertices")]
    pub fn vertices(&self) -> (Vec3, Vec3, Vec3) {
        unsafe {
            let mut a = Vec3::uninitialized();
            let mut b = Vec3::uninitialized();
            let mut c = Vec3::uninitialized();
            ffi::graphene_triangle_get_vertices(
                self.to_glib_none().0,
                a.to_glib_none_mut().0,
                b.to_glib_none_mut().0,
                c.to_glib_none_mut().0,
            );
            (a, b, c)
        }
    }

    //#[doc(alias = "graphene_triangle_init_from_float")]
    //pub fn init_from_float(&mut self, a: /*Unimplemented*/FixedArray TypeId { ns_id: 0, id: 20 }; 3, b: /*Unimplemented*/FixedArray TypeId { ns_id: 0, id: 20 }; 3, c: /*Unimplemented*/FixedArray TypeId { ns_id: 0, id: 20 }; 3) -> Option<Triangle> {
    //    unsafe { TODO: call ffi:graphene_triangle_init_from_float() }
    //}

    /// Initializes a [`Triangle`][crate::Triangle] using the three given 3D points.
    /// ## `a`
    /// a [`Point3D`][crate::Point3D]
    /// ## `b`
    /// a [`Point3D`][crate::Point3D]
    /// ## `c`
    /// a [`Point3D`][crate::Point3D]
    ///
    /// # Returns
    ///
    /// the initialized [`Triangle`][crate::Triangle]
    #[doc(alias = "graphene_triangle_init_from_point3d")]
    pub fn init_from_point3d(
        &mut self,
        a: Option<&Point3D>,
        b: Option<&Point3D>,
        c: Option<&Point3D>,
    ) {
        unsafe {
            ffi::graphene_triangle_init_from_point3d(
                self.to_glib_none_mut().0,
                a.to_glib_none().0,
                b.to_glib_none().0,
                c.to_glib_none().0,
            );
        }
    }

    /// Initializes a [`Triangle`][crate::Triangle] using the three given vectors.
    /// ## `a`
    /// a [`Vec3`][crate::Vec3]
    /// ## `b`
    /// a [`Vec3`][crate::Vec3]
    /// ## `c`
    /// a [`Vec3`][crate::Vec3]
    ///
    /// # Returns
    ///
    /// the initialized [`Triangle`][crate::Triangle]
    #[doc(alias = "graphene_triangle_init_from_vec3")]
    pub fn init_from_vec3(&mut self, a: Option<&Vec3>, b: Option<&Vec3>, c: Option<&Vec3>) {
        unsafe {
            ffi::graphene_triangle_init_from_vec3(
                self.to_glib_none_mut().0,
                a.to_glib_none().0,
                b.to_glib_none().0,
                c.to_glib_none().0,
            );
        }
    }
}

impl PartialEq for Triangle {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.equal(other)
    }
}

impl Eq for Triangle {}