mic_fast_calc.pyx 5.75 KB
Newer Older
eric pellegrini's avatar
eric pellegrini committed
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
cimport numpy as np 
import numpy as np
from numpy cimport ndarray

from libcpp.vector cimport vector
from libcpp.pair cimport pair 
from libcpp cimport bool

ctypedef pair[ int, vector[int] ] equiv_t
ctypedef vector[double] point

cdef inline bool exclude_from_bounded_3Dbox(double x,
                                            double y,
                                            double z, 
                                            double Xmin, 
                                            double Xmax, 
                                            double Ymin, 
                                            double Ymax, 
                                            double Zmin, 
                                            double Zmax, 
                                            double border): 
    return not ((Xmin-border <=x) and  (x <= Xmax+border) and (Ymin-border <= y) and (y <= Ymax+border) and (Zmin-border <= z) and (z <= Zmax+border)) 

cdef inline bool exclude_from_bounded_2Dbox(double x,
                                            double y,
                                            double Xmin, 
                                            double Xmax, 
                                            double Ymin, 
                                            double Ymax, 
                                            double border): 
    return not ((Xmin-border <=x) and  (x <= Xmax+border) and (Ymin-border <= y) and (y <= Ymax+border))


 
def mic_generator_2D(double[:,:] pts not None, double border, double[:] box_param):

    cdef int i, m, new_id, nb_init_points, nb_final_points
    cdef double x, y, Xmin, Xmax, Ymin, Ymax, j, k, n0, n1
    cdef vector[equiv_t] equivalence
    cdef vector[point] new_points
    cdef equiv_t e
    cdef point n
    cdef double lol[3]
    cdef ndarray[np.float64_t, ndim = 2] res
    
    nb_init_points = pts.shape[0]
    
    Xmax = box_param[0]
    Ymax = box_param[1]
    Xmin = 0
    Ymin = 0 
    
    lol[0] = -1
    lol[1] = 0
    lol[2] = 1
    
    for i in range(nb_init_points):
        x = pts[i,0]
        y = pts[i,1]
        for j in lol[0:3]:
            for k in lol[0:3]:
                if j == 0 and k == 0:
                    continue
                n0 = x+j*(Xmax-Xmin)
                n1 = y+k*(Ymax-Ymin)
                
                if exclude_from_bounded_2Dbox(n0, n1, Xmin, Xmax, Ymin, Ymax, border):
                    continue
                n = point()
                n.push_back(n0)
                n.push_back(n1)
                new_points.push_back(n)
                new_id = new_points.size() + nb_init_points
                for m in range(equivalence.size()):
                    if equivalence[m].first == i:
                        equivalence[m].second.push_back(new_id)
                        continue
                    e = equiv_t()
                    e.first=i
                    e.second.push_back(new_id)
                    equivalence.push_back(e)
    d = {}
    for i in range(equivalence.size()):
        d[equivalence[i].first] = <list> equivalence[i].second
        
    nb_final_points = pts.shape[0] + new_points.size()
    
    res = np.zeros((nb_init_points + new_points.size() ,2), dtype = np.float64)

    for i in range(nb_init_points):
        res[i,0] = pts[i,0]
        res[i,1] = pts[i,1]
    
    for i in range(new_points.size()):
        j = i + nb_init_points
        res[j,0] = new_points[i][0]
        res[j,1] = new_points[i][1]
    
    
    return res, d

        
        
def mic_generator_3D(double[:,:] pts not None, double border, double[:] box_param):
    
106
    cdef int i, j_int, m, new_id, nb_init_points
eric pellegrini's avatar
eric pellegrini committed
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
    cdef double x, y, z, Xmin, Xmax, Ymin, Ymax, Zmin, Zmax, j, k, l, n0, n1, n2
    cdef vector[equiv_t] equivalence
    cdef vector[point] new_points
    cdef equiv_t e
    cdef point n
    cdef double lol[3]
    cdef ndarray[np.float64_t, ndim = 2] res
    
    nb_init_points = pts.shape[0]
    
    Xmax = box_param[0]
    Ymax = box_param[1]
    Zmax = box_param[2] 
    Xmin = 0
    Ymin = 0 
    Zmin = 0

    lol[0] = -1
    lol[1] = 0
    lol[2] = 1
    
    for i in range(nb_init_points):
        x = pts[i,0]
        y = pts[i,1]
        z = pts[i,2]
        for j in lol[0:3]:
            for k in lol[0:3]:
                for l in lol[0:3]:
                    if j == 0 and k == 0 and l == 0:
                        continue
                    n0 = x+j*(Xmax-Xmin)
                    n1 = y+k*(Ymax-Ymin)
                    n2 = z+l*(Zmax-Zmin)
                    
                    if exclude_from_bounded_3Dbox(n0, n1, n2, Xmin, Xmax, Ymin, Ymax, Zmin, Zmax, border):
                        continue
                    n = point()
                    n.push_back(n0)
                    n.push_back(n1)
                    n.push_back(n2)
                    new_points.push_back(n)
                    new_id = new_points.size() + nb_init_points
                    for m in range(equivalence.size()):
                        if equivalence[m].first == i:
                            equivalence[m].second.push_back(new_id)
                            continue
                        e = equiv_t()
                        e.first=i
                        e.second.push_back(new_id)
                        equivalence.push_back(e)

    d = {}
    for i in range(equivalence.size()):
        d[equivalence[i].first] = <list> equivalence[i].second

    res = np.zeros((nb_init_points + new_points.size() ,3), dtype = np.float64)

    for i in range(nb_init_points):
        res[i,0] = pts[i,0]
        res[i,1] = pts[i,1]
        res[i,2] = pts[i,2]
    
    for i in range(new_points.size()):
        j = i + nb_init_points
171
172
173
174
        j_int = int(j)
        res[j_int,0] = new_points[i][0]
        res[j_int,1] = new_points[i][1]
        res[j_int,2] = new_points[i][2]
eric pellegrini's avatar
eric pellegrini committed
175
176
    
    return res, d