Changeset fdc8e40 in mainline


Ignore:
Timestamp:
2019-12-13T22:30:30Z (5 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3434233
Parents:
4d9c807
Message:

gfx_rect_clip, gfx_rect_rtranslate

Location:
uspace/lib/gfx
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/gfx/include/gfx/coord.h

    r4d9c807 rfdc8e40  
    4545    gfx_coord_t *);
    4646extern void gfx_rect_translate(gfx_coord2_t *, gfx_rect_t *, gfx_rect_t *);
     47extern void gfx_rect_rtranslate(gfx_coord2_t *, gfx_rect_t *, gfx_rect_t *);
    4748extern void gfx_rect_envelope(gfx_rect_t *, gfx_rect_t *, gfx_rect_t *);
     49extern void gfx_rect_clip(gfx_rect_t *, gfx_rect_t *, gfx_rect_t *);
    4850extern void gfx_rect_points_sort(gfx_rect_t *, gfx_rect_t *);
    4951extern bool gfx_rect_is_empty(gfx_rect_t *);
  • uspace/lib/gfx/src/coord.c

    r4d9c807 rfdc8e40  
    8787/** Move (translate) rectangle.
    8888 *
    89  * @param trans Translation
     89 * @param trans Translation offset
    9090 * @param src Source rectangle
    9191 * @param dest Destination rectangle
     
    9393void gfx_rect_translate(gfx_coord2_t *trans, gfx_rect_t *src, gfx_rect_t *dest)
    9494{
    95         gfx_coord2_add(trans, &src->p0, &dest->p0);
    96         gfx_coord2_add(trans, &src->p1, &dest->p1);
     95        gfx_coord2_add(&src->p0, trans, &dest->p0);
     96        gfx_coord2_add(&src->p1, trans, &dest->p1);
     97}
     98
     99/** Reverse move (translate) rectangle.
     100 *
     101 * @param trans Translation offset
     102 * @param src Source rectangle
     103 * @param dest Destination rectangle
     104 */
     105void gfx_rect_rtranslate(gfx_coord2_t *trans, gfx_rect_t *src, gfx_rect_t *dest)
     106{
     107        gfx_coord2_subtract(&src->p0, trans, &dest->p0);
     108        gfx_coord2_subtract(&src->p1, trans, &dest->p1);
    97109}
    98110
     
    100112 *
    101113 * Envelope is the minimal rectangle covering all pixels of both rectangles.
     114 *
     115 * @param a First rectangle
     116 * @param b Second rectangle
     117 * @param dest Place to store enveloping rectangle
    102118 */
    103119void gfx_rect_envelope(gfx_rect_t *a, gfx_rect_t *b, gfx_rect_t *dest)
     
    124140        dest->p1.x = max(sa.p1.x, sb.p1.x);
    125141        dest->p1.y = max(sa.p1.y, sb.p1.y);
     142}
     143
     144/** Compute intersection of two rectangles.
     145 *
     146 * If the two rectangles do not intersect, the result will be an empty
     147 * rectangle (check with gfx_rect_is_empty()).
     148 *
     149 * @param rect Source rectangle
     150 * @param clip Clipping rectangle
     151 * @param dest Place to store clipped rectangle
     152 */
     153void gfx_rect_clip(gfx_rect_t *rect, gfx_rect_t *clip, gfx_rect_t *dest)
     154{
     155        gfx_rect_t srect, sclip;
     156
     157        gfx_rect_points_sort(rect, &srect);
     158        gfx_rect_points_sort(clip, &sclip);
     159
     160        dest->p0.x = min(max(srect.p0.x, sclip.p0.x), sclip.p1.x);
     161        dest->p0.y = min(max(srect.p0.y, sclip.p0.y), sclip.p1.y);
     162        dest->p1.x = max(sclip.p0.x, min(srect.p1.x, sclip.p1.x));
     163        dest->p1.y = max(sclip.p0.y, min(srect.p1.y, sclip.p1.y));
    126164}
    127165
  • uspace/lib/gfx/test/coord.c

    r4d9c807 rfdc8e40  
    3434PCUT_TEST_SUITE(coord);
    3535
    36 extern void gfx_coord2_add(gfx_coord2_t *, gfx_coord2_t *, gfx_coord2_t *);
    37 extern void gfx_coord2_subtract(gfx_coord2_t *, gfx_coord2_t *, gfx_coord2_t *);
    38 extern void gfx_rect_translate(gfx_coord2_t *, gfx_rect_t *, gfx_rect_t *);
    39 
    4036/** gfx_coord2_add should add two coordinate vectors */
    4137PCUT_TEST(coord2_add)
     
    5147        gfx_coord2_add(&a, &b, &d);
    5248
    53         PCUT_ASSERT_EQUALS(a.x + b.x, d.x);
    54         PCUT_ASSERT_EQUALS(a.y + b.y, d.y);
     49        PCUT_ASSERT_INT_EQUALS(a.x + b.x, d.x);
     50        PCUT_ASSERT_INT_EQUALS(a.y + b.y, d.y);
    5551}
    5652
     
    6864        gfx_coord2_subtract(&a, &b, &d);
    6965
    70         PCUT_ASSERT_EQUALS(a.x - b.x, d.x);
    71         PCUT_ASSERT_EQUALS(a.y - b.y, d.y);
     66        PCUT_ASSERT_INT_EQUALS(a.x - b.x, d.x);
     67        PCUT_ASSERT_INT_EQUALS(a.y - b.y, d.y);
    7268}
    7369
     
    8985        gfx_rect_translate(&offs, &srect, &drect);
    9086
    91         PCUT_ASSERT_EQUALS(offs.x + srect.p0.x, drect.p0.x);
    92         PCUT_ASSERT_EQUALS(offs.y + srect.p0.y, drect.p0.y);
    93         PCUT_ASSERT_EQUALS(offs.x + srect.p1.x, drect.p1.x);
    94         PCUT_ASSERT_EQUALS(offs.y + srect.p1.y, drect.p1.y);
     87        PCUT_ASSERT_INT_EQUALS(offs.x + srect.p0.x, drect.p0.x);
     88        PCUT_ASSERT_INT_EQUALS(offs.y + srect.p0.y, drect.p0.y);
     89        PCUT_ASSERT_INT_EQUALS(offs.x + srect.p1.x, drect.p1.x);
     90        PCUT_ASSERT_INT_EQUALS(offs.y + srect.p1.y, drect.p1.y);
     91}
     92
     93/** gfx_rect_rtranslate should reverse-translate rectangle */
     94PCUT_TEST(rect_rtranslate)
     95{
     96        gfx_coord2_t offs;
     97        gfx_rect_t srect;
     98        gfx_rect_t drect;
     99
     100        offs.x = 5;
     101        offs.y = 6;
     102
     103        srect.p0.x = 10;
     104        srect.p0.y = 11;
     105        srect.p1.x = 20;
     106        srect.p1.y = 22;
     107
     108        gfx_rect_rtranslate(&offs, &srect, &drect);
     109
     110        PCUT_ASSERT_INT_EQUALS(srect.p0.x - offs.x, drect.p0.x);
     111        PCUT_ASSERT_INT_EQUALS(srect.p0.y - offs.y, drect.p0.y);
     112        PCUT_ASSERT_INT_EQUALS(srect.p1.x - offs.x, drect.p1.x);
     113        PCUT_ASSERT_INT_EQUALS(srect.p1.y - offs.y, drect.p1.y);
    95114}
    96115
     
    101120
    102121        gfx_span_points_sort(1, 2, &a, &b);
    103         PCUT_ASSERT_EQUALS(1, a);
    104         PCUT_ASSERT_EQUALS(2, b);
     122        PCUT_ASSERT_INT_EQUALS(1, a);
     123        PCUT_ASSERT_INT_EQUALS(2, b);
    105124}
    106125
     
    111130
    112131        gfx_span_points_sort(1, 1, &a, &b);
    113         PCUT_ASSERT_EQUALS(1, a);
    114         PCUT_ASSERT_EQUALS(1, b);
     132        PCUT_ASSERT_INT_EQUALS(1, a);
     133        PCUT_ASSERT_INT_EQUALS(1, b);
    115134}
    116135
     
    121140
    122141        gfx_span_points_sort(1, 0, &a, &b);
    123         PCUT_ASSERT_EQUALS(1, a);
    124         PCUT_ASSERT_EQUALS(2, b);
     142        PCUT_ASSERT_INT_EQUALS(1, a);
     143        PCUT_ASSERT_INT_EQUALS(2, b);
    125144}
    126145
     
    143162
    144163        gfx_rect_envelope(&a, &b, &e);
    145         PCUT_ASSERT_EQUALS(1, e.p0.x);
    146         PCUT_ASSERT_EQUALS(2, e.p0.y);
    147         PCUT_ASSERT_EQUALS(3, e.p1.x);
    148         PCUT_ASSERT_EQUALS(4, e.p1.y);
     164        PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
     165        PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
     166        PCUT_ASSERT_INT_EQUALS(3, e.p1.x);
     167        PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
    149168}
    150169
     
    167186
    168187        gfx_rect_envelope(&a, &b, &e);
    169         PCUT_ASSERT_EQUALS(1, e.p0.x);
    170         PCUT_ASSERT_EQUALS(2, e.p0.y);
    171         PCUT_ASSERT_EQUALS(3, e.p1.x);
    172         PCUT_ASSERT_EQUALS(4, e.p1.y);
     188        PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
     189        PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
     190        PCUT_ASSERT_INT_EQUALS(3, e.p1.x);
     191        PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
    173192}
    174193
     
    191210
    192211        gfx_rect_envelope(&a, &b, &e);
    193         PCUT_ASSERT_EQUALS(1, e.p0.x);
    194         PCUT_ASSERT_EQUALS(2, e.p0.y);
    195         PCUT_ASSERT_EQUALS(7, e.p1.x);
    196         PCUT_ASSERT_EQUALS(8, e.p1.y);
     212        PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
     213        PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
     214        PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
     215        PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
    197216}
    198217
     
    215234
    216235        gfx_rect_envelope(&a, &b, &e);
    217         PCUT_ASSERT_EQUALS(1, e.p0.x);
    218         PCUT_ASSERT_EQUALS(2, e.p0.y);
    219         PCUT_ASSERT_EQUALS(7, e.p1.x);
    220         PCUT_ASSERT_EQUALS(8, e.p1.y);
     236        PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
     237        PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
     238        PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
     239        PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
    221240}
    222241
     
    239258
    240259        gfx_rect_envelope(&a, &b, &e);
    241         PCUT_ASSERT_EQUALS(1, e.p0.x);
    242         PCUT_ASSERT_EQUALS(2, e.p0.y);
    243         PCUT_ASSERT_EQUALS(7, e.p1.x);
    244         PCUT_ASSERT_EQUALS(8, e.p1.y);
     260        PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
     261        PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
     262        PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
     263        PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
    245264}
    246265
     
    263282
    264283        gfx_rect_envelope(&a, &b, &e);
    265         PCUT_ASSERT_EQUALS(1, e.p0.x);
    266         PCUT_ASSERT_EQUALS(2, e.p0.y);
    267         PCUT_ASSERT_EQUALS(7, e.p1.x);
    268         PCUT_ASSERT_EQUALS(8, e.p1.y);
     284        PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
     285        PCUT_ASSERT_INT_EQUALS(2, e.p0.y);
     286        PCUT_ASSERT_INT_EQUALS(7, e.p1.x);
     287        PCUT_ASSERT_INT_EQUALS(8, e.p1.y);
    269288}
    270289
     
    287306
    288307        gfx_rect_envelope(&a, &b, &e);
    289         PCUT_ASSERT_EQUALS(1, e.p0.x);
    290         PCUT_ASSERT_EQUALS(1, e.p0.y);
    291         PCUT_ASSERT_EQUALS(4, e.p1.x);
    292         PCUT_ASSERT_EQUALS(4, e.p1.y);
     308        PCUT_ASSERT_INT_EQUALS(1, e.p0.x);
     309        PCUT_ASSERT_INT_EQUALS(1, e.p0.y);
     310        PCUT_ASSERT_INT_EQUALS(4, e.p1.x);
     311        PCUT_ASSERT_INT_EQUALS(4, e.p1.y);
     312}
     313
     314/** Clip rectangle with rect completely inside the clipping rectangle */
     315PCUT_TEST(rect_clip_rect_inside)
     316{
     317        gfx_rect_t rect;
     318        gfx_rect_t clip;
     319        gfx_rect_t dest;
     320
     321        rect.p0.x = 3;
     322        rect.p0.y = 4;
     323        rect.p1.x = 5;
     324        rect.p1.y = 6;
     325
     326        clip.p0.x = 1;
     327        clip.p0.y = 2;
     328        clip.p1.x = 7;
     329        clip.p1.y = 8;
     330
     331        gfx_rect_clip(&rect, &clip, &dest);
     332        PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
     333        PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
     334        PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
     335        PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
     336}
     337
     338/** Clip rectangle with rect covering the clipping rectangle */
     339PCUT_TEST(rect_clip_rect_covering)
     340{
     341        gfx_rect_t rect;
     342        gfx_rect_t clip;
     343        gfx_rect_t dest;
     344
     345        rect.p0.x = 1;
     346        rect.p0.y = 2;
     347        rect.p1.x = 7;
     348        rect.p1.y = 8;
     349
     350        clip.p0.x = 3;
     351        clip.p0.y = 4;
     352        clip.p1.x = 5;
     353        clip.p1.y = 6;
     354
     355        gfx_rect_clip(&rect, &clip, &dest);
     356        PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
     357        PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
     358        PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
     359        PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
     360}
     361
     362/** Clip rectangle with rect outside, having lower coordinates */
     363PCUT_TEST(rect_clip_rect_out_ll)
     364{
     365        gfx_rect_t rect;
     366        gfx_rect_t clip;
     367        gfx_rect_t dest;
     368
     369        rect.p0.x = 1;
     370        rect.p0.y = 2;
     371        rect.p1.x = 3;
     372        rect.p1.y = 4;
     373
     374        clip.p0.x = 5;
     375        clip.p0.y = 6;
     376        clip.p1.x = 7;
     377        clip.p1.y = 8;
     378
     379        gfx_rect_clip(&rect, &clip, &dest);
     380        PCUT_ASSERT_INT_EQUALS(5, dest.p0.x);
     381        PCUT_ASSERT_INT_EQUALS(6, dest.p0.y);
     382        PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
     383        PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
     384}
     385
     386/** Clip rectangle with rect outside, having higher coordinates */
     387PCUT_TEST(rect_clip_rect_out_hh)
     388{
     389        gfx_rect_t rect;
     390        gfx_rect_t clip;
     391        gfx_rect_t dest;
     392
     393        rect.p0.x = 5;
     394        rect.p0.y = 6;
     395        rect.p1.x = 7;
     396        rect.p1.y = 8;
     397
     398        clip.p0.x = 1;
     399        clip.p0.y = 2;
     400        clip.p1.x = 3;
     401        clip.p1.y = 4;
     402
     403        gfx_rect_clip(&rect, &clip, &dest);
     404        PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
     405        PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
     406        PCUT_ASSERT_INT_EQUALS(3, dest.p1.x);
     407        PCUT_ASSERT_INT_EQUALS(4, dest.p1.y);
     408}
     409
     410/** Clip rectangle with rect partially outside, having lower coordinates */
     411PCUT_TEST(rect_clip_rect_ll)
     412{
     413        gfx_rect_t rect;
     414        gfx_rect_t clip;
     415        gfx_rect_t dest;
     416
     417        rect.p0.x = 1;
     418        rect.p0.y = 2;
     419        rect.p1.x = 5;
     420        rect.p1.y = 6;
     421
     422        clip.p0.x = 3;
     423        clip.p0.y = 4;
     424        clip.p1.x = 7;
     425        clip.p1.y = 8;
     426
     427        gfx_rect_clip(&rect, &clip, &dest);
     428        PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
     429        PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
     430        PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
     431        PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
     432}
     433
     434/** Clip rectangle with rect partially outside, having higher coordinates */
     435PCUT_TEST(rect_clip_rect_hh)
     436{
     437        gfx_rect_t rect;
     438        gfx_rect_t clip;
     439        gfx_rect_t dest;
     440
     441        rect.p0.x = 3;
     442        rect.p0.y = 4;
     443        rect.p1.x = 7;
     444        rect.p1.y = 8;
     445
     446        clip.p0.x = 1;
     447        clip.p0.y = 2;
     448        clip.p1.x = 5;
     449        clip.p1.y = 6;
     450
     451        gfx_rect_clip(&rect, &clip, &dest);
     452        PCUT_ASSERT_INT_EQUALS(3, dest.p0.x);
     453        PCUT_ASSERT_INT_EQUALS(4, dest.p0.y);
     454        PCUT_ASSERT_INT_EQUALS(5, dest.p1.x);
     455        PCUT_ASSERT_INT_EQUALS(6, dest.p1.y);
    293456}
    294457
     
    299462
    300463        gfx_span_points_sort(1, 2, &s0, &s1);
    301         PCUT_ASSERT_EQUALS(1, s0);
    302         PCUT_ASSERT_EQUALS(2, s1);
     464        PCUT_ASSERT_INT_EQUALS(1, s0);
     465        PCUT_ASSERT_INT_EQUALS(2, s1);
    303466}
    304467
     
    309472
    310473        gfx_span_points_sort(2, 1, &s0, &s1);
    311         PCUT_ASSERT_EQUALS(2, s0);
    312         PCUT_ASSERT_EQUALS(3, s1);
     474        PCUT_ASSERT_INT_EQUALS(2, s0);
     475        PCUT_ASSERT_INT_EQUALS(3, s1);
    313476}
    314477
Note: See TracChangeset for help on using the changeset viewer.