Welcome to mirror list, hosted at ThFree Co, Russian Federation.

agg_clip_liang_barsky.h « agg « src - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b5fedbab5f6149a4881b863b9f26ccaabf9a2cc (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
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
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software 
// is granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
//          mcseemagg@yahoo.com
//          http://www.antigrain.com
//----------------------------------------------------------------------------
//
// Liang-Barsky clipping 
//
//----------------------------------------------------------------------------
#ifndef AGG_CLIP_LIANG_BARSKY_INCLUDED
#define AGG_CLIP_LIANG_BARSKY_INCLUDED

#include "agg_basics.h"

namespace agg
{

    //------------------------------------------------------------------------
    enum clipping_flags_e
    {
        clipping_flags_x1_clipped = 4,
        clipping_flags_x2_clipped = 1,
        clipping_flags_y1_clipped = 8,
        clipping_flags_y2_clipped = 2,
        clipping_flags_x_clipped = clipping_flags_x1_clipped | clipping_flags_x2_clipped,
        clipping_flags_y_clipped = clipping_flags_y1_clipped | clipping_flags_y2_clipped
    };

    //----------------------------------------------------------clipping_flags
    // Determine the clipping code of the vertex according to the 
    // Cyrus-Beck line clipping algorithm
    //
    //        |        |
    //  0110  |  0010  | 0011
    //        |        |
    // -------+--------+-------- clip_box.y2
    //        |        |
    //  0100  |  0000  | 0001
    //        |        |
    // -------+--------+-------- clip_box.y1
    //        |        |
    //  1100  |  1000  | 1001
    //        |        |
    //  clip_box.x1  clip_box.x2
    //
    // 
    template<class T>
    inline unsigned clipping_flags(T x, T y, const rect_base<T>& clip_box)
    {
        return  (x > clip_box.x2) |
               ((y > clip_box.y2) << 1) |
               ((x < clip_box.x1) << 2) |
               ((y < clip_box.y1) << 3);
    }

    //--------------------------------------------------------clipping_flags_x
    template<class T>
    inline unsigned clipping_flags_x(T x, const rect_base<T>& clip_box)
    {
        return  (x > clip_box.x2) | ((x < clip_box.x1) << 2);
    }


    //--------------------------------------------------------clipping_flags_y
    template<class T>
    inline unsigned clipping_flags_y(T y, const rect_base<T>& clip_box)
    {
        return ((y > clip_box.y2) << 1) | ((y < clip_box.y1) << 3);
    }


    //-------------------------------------------------------clip_liang_barsky
    template<class T>
    inline unsigned clip_liang_barsky(T x1, T y1, T x2, T y2,
                                      const rect_base<T>& clip_box,
                                      T* x, T* y)
    {
        const double nearzero = 1e-30;

        double deltax = x2 - x1;
        double deltay = y2 - y1; 
        double xin;
        double xout;
        double yin;
        double yout;
        double tinx;
        double tiny;
        double toutx;
        double touty;  
        double tin1;
        double tin2;
        double tout1;
        unsigned np = 0;

        if(deltax == 0.0) 
        {   
            // bump off of the vertical
            deltax = (x1 > clip_box.x1) ? -nearzero : nearzero;
        }

        if(deltay == 0.0) 
        { 
            // bump off of the horizontal 
            deltay = (y1 > clip_box.y1) ? -nearzero : nearzero;
        }
        
        if(deltax > 0.0) 
        {                
            // points to right
            xin  = clip_box.x1;
            xout = clip_box.x2;
        }
        else 
        {
            xin  = clip_box.x2;
            xout = clip_box.x1;
        }

        if(deltay > 0.0) 
        {
            // points up
            yin  = clip_box.y1;
            yout = clip_box.y2;
        }
        else 
        {
            yin  = clip_box.y2;
            yout = clip_box.y1;
        }
        
        tinx = (xin - x1) / deltax;
        tiny = (yin - y1) / deltay;
        
        if (tinx < tiny) 
        {
            // hits x first
            tin1 = tinx;
            tin2 = tiny;
        }
        else
        {
            // hits y first
            tin1 = tiny;
            tin2 = tinx;
        }
        
        if(tin1 <= 1.0) 
        {
            if(0.0 < tin1) 
            {
                *x++ = (T)xin;
                *y++ = (T)yin;
                ++np;
            }

            if(tin2 <= 1.0)
            {
                toutx = (xout - x1) / deltax;
                touty = (yout - y1) / deltay;
                
                tout1 = (toutx < touty) ? toutx : touty;
                
                if(tin2 > 0.0 || tout1 > 0.0) 
                {
                    if(tin2 <= tout1) 
                    {
                        if(tin2 > 0.0) 
                        {
                            if(tinx > tiny) 
                            {
                                *x++ = (T)xin;
                                *y++ = (T)(y1 + tinx * deltay);
                            }
                            else 
                            {
                                *x++ = (T)(x1 + tiny * deltax);
                                *y++ = (T)yin;
                            }
                            ++np;
                        }

                        if(tout1 < 1.0) 
                        {
                            if(toutx < touty) 
                            {
                                *x++ = (T)xout;
                                *y++ = (T)(y1 + toutx * deltay);
                            }
                            else 
                            {
                                *x++ = (T)(x1 + touty * deltax);
                                *y++ = (T)yout;
                            }
                        }
                        else 
                        {
                            *x++ = x2;
                            *y++ = y2;
                        }
                        ++np;
                    }
                    else 
                    {
                        if(tinx > tiny) 
                        {
                            *x++ = (T)xin;
                            *y++ = (T)yout;
                        }
                        else 
                        {
                            *x++ = (T)xout;
                            *y++ = (T)yin;
                        }
                        ++np;
                    }
                }
            }
        }
        return np;
    }


    //----------------------------------------------------------------------------
    template<class T>
    bool clip_move_point(T x1, T y1, T x2, T y2, 
                         const rect_base<T>& clip_box, 
                         T* x, T* y, unsigned flags)
    {
       T bound;

       if(flags & clipping_flags_x_clipped)
       {
           if(x1 == x2)
           {
               return false;
           }
           bound = (flags & clipping_flags_x1_clipped) ? clip_box.x1 : clip_box.x2;
           *y = (T)(double(bound - x1) * (y2 - y1) / (x2 - x1) + y1);
           *x = bound;
       }

       flags = clipping_flags_y(*y, clip_box);
       if(flags & clipping_flags_y_clipped)
       {
           if(y1 == y2)
           {
               return false;
           }
           bound = (flags & clipping_flags_y1_clipped) ? clip_box.y1 : clip_box.y2;
           *x = (T)(double(bound - y1) * (x2 - x1) / (y2 - y1) + x1);
           *y = bound;
       }
       return true;
    }

    //-------------------------------------------------------clip_line_segment
    // Returns: ret >= 4        - Fully clipped
    //          (ret & 1) != 0  - First point has been moved
    //          (ret & 2) != 0  - Second point has been moved
    //
    template<class T>
    unsigned clip_line_segment(T* x1, T* y1, T* x2, T* y2,
                               const rect_base<T>& clip_box)
    {
        unsigned f1 = clipping_flags(*x1, *y1, clip_box);
        unsigned f2 = clipping_flags(*x2, *y2, clip_box);
        unsigned ret = 0;

        if((f2 | f1) == 0)
        {
            // Fully visible
            return 0;
        }

        if((f1 & clipping_flags_x_clipped) != 0 && 
           (f1 & clipping_flags_x_clipped) == (f2 & clipping_flags_x_clipped))
        {
            // Fully clipped
            return 4;
        }

        if((f1 & clipping_flags_y_clipped) != 0 && 
           (f1 & clipping_flags_y_clipped) == (f2 & clipping_flags_y_clipped))
        {
            // Fully clipped
            return 4;
        }

        T tx1 = *x1;
        T ty1 = *y1;
        T tx2 = *x2;
        T ty2 = *y2;
        if(f1) 
        {   
            if(!clip_move_point(tx1, ty1, tx2, ty2, clip_box, x1, y1, f1)) 
            {
                return 4;
            }
            if(*x1 == *x2 && *y1 == *y2) 
            {
                return 4;
            }
            ret |= 1;
        }
        if(f2) 
        {
            if(!clip_move_point(tx1, ty1, tx2, ty2, clip_box, x2, y2, f2))
            {
                return 4;
            }
            if(*x1 == *x2 && *y1 == *y2) 
            {
                return 4;
            }
            ret |= 2;
        }
        return ret;
    }


}


#endif