Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief définition d'une facette
5"""
6import math
7from .image_synthese_base import Objet
10class Facette (Objet):
11 """définit un triangle dans l'espace"""
13 def __init__(self, a, b, c, couleur):
14 """initialisation"""
15 self.a, self.b, self.c = a, b, c
16 ab = b - a
17 ac = c - a
18 self.vnorm = ab.vectoriel(ac)
19 self.vnorm = self.vnorm.renorme()
20 self.couleur = couleur
22 def intersection_plan(self, r):
23 """retourne le point d'intersection entre le plan et le rayon r"""
24 if r.direction.scalaire(self.vnorm) == 0:
25 return None
26 oa = self.a - r.origine
27 lv = self.vnorm.scalaire(oa) / self.vnorm.scalaire(r.direction)
28 p = r.origine + r.direction * lv
29 return p
31 def point_interieur(self, p):
32 """dit si un point appartient à l'intérieur du triangle"""
33 pa = self.a - p
34 pb = self.b - p
35 pc = self.c - p
36 theta = pa.angle(pb, self.vnorm)
37 theta += pb.angle(pc, self.vnorm)
38 theta += pc.angle(pa, self.vnorm)
39 theta = abs(theta)
40 return theta >= math.pi * 0.9
42 def intersection(self, r):
43 """retourne le point d'intersection avec le rayon r,
44 retourne None s'il n'y pas d'intersection"""
45 p = self.intersection_plan(r)
46 if p is None:
47 return None
48 if self.point_interieur(p):
49 return p
50 else:
51 return None
53 def normale(self, p, rayon):
54 """retourne la normale au point de coordonnée p et connaissant le rayon"""
55 if rayon.direction.scalaire(self.vnorm) < 0:
56 return self.vnorm
57 else:
58 return - self.vnorm
60 def couleur_point(self, p):
61 """retourne la couleur au point de coordonnée p"""
62 return self.couleur
64 def __str__(self):
65 """affichage"""
66 s = "facette --- a : " + str(self.a)
67 s += " b : " + str(self.b)
68 s += " c : " + str(self.c)
69 s += " couleur : " + str(self.couleur)
70 return s
73class Rectangle (Facette):
74 """définit un rectangle dans l'espace"""
76 def __init__(self, a, b, c, d, couleur):
77 """initialisation, si d == None, d est calculé comme étant
78 le symétrique de b par rapport au milieu du segment [ac]"""
79 Facette.__init__(self, a, b, c, couleur)
80 if d is not None:
81 self.d = d
82 else:
83 i = (a + c) / 2
84 self.d = b + (i - b) * 2
86 def point_interieur(self, p):
87 """dit si un point appartient à l'intérieur du triangle"""
88 pa = self.a - p
89 pb = self.b - p
90 pc = self.c - p
91 pd = self.d - p
92 theta = pa.angle(pb, self.vnorm)
93 theta += pb.angle(pc, self.vnorm)
94 theta += pc.angle(pd, self.vnorm)
95 theta += pd.angle(pa, self.vnorm)
96 theta = abs(theta)
97 return theta >= math.pi * 0.9
99 def __str__(self):
100 """affichage"""
101 s = "rectangle --- a : " + str(self.a)
102 s += " b : " + str(self.b)
103 s += " c : " + str(self.c)
104 s += " d : " + str(self.d)
105 s += " couleur : " + str(self.couleur)
106 return s