Blender V2.61 - r43446

rct.c

Go to the documentation of this file.
00001 /*
00002  * 
00003  * rct.c
00004  * 
00005  * april 95
00006  * 
00007  *
00008  * A minimalist lib for functions doing stuff with rectangle structs.
00009  *
00010  * ***** BEGIN GPL LICENSE BLOCK *****
00011  *
00012  * This program is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU General Public License
00014  * as published by the Free Software Foundation; either version 2
00015  * of the License, or (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software Foundation,
00024  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00025  *
00026  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00027  * All rights reserved.
00028  *
00029  * The Original Code is: all of this file.
00030  *
00031  * Contributor(s): none yet.
00032  *
00033  * ***** END GPL LICENSE BLOCK *****
00034  *
00035  */
00036 
00041 #include <stdio.h>
00042 #include <math.h>
00043 
00044 #include "DNA_vec_types.h"
00045 #include "BLI_rect.h"
00046 
00047 int BLI_rcti_is_empty(rcti * rect)
00048 {
00049     return ((rect->xmax<=rect->xmin) ||
00050             (rect->ymax<=rect->ymin));
00051 }
00052 
00053 int BLI_rctf_is_empty(rctf * rect)
00054 {
00055     return ((rect->xmax<=rect->xmin) ||
00056             (rect->ymax<=rect->ymin));
00057 }
00058 
00059 int BLI_in_rcti(rcti * rect, int x, int y)
00060 {
00061 
00062     if(x<rect->xmin) return 0;
00063     if(x>rect->xmax) return 0;
00064     if(y<rect->ymin) return 0;
00065     if(y>rect->ymax) return 0;
00066     return 1;
00067 }
00068 
00069 int BLI_in_rctf(rctf *rect, float x, float y)
00070 {
00071 
00072     if(x<rect->xmin) return 0;
00073     if(x>rect->xmax) return 0;
00074     if(y<rect->ymin) return 0;
00075     if(y>rect->ymax) return 0;
00076     return 1;
00077 }
00078 
00079 void BLI_union_rctf(rctf *rct1, rctf *rct2)
00080 {
00081     
00082     if(rct1->xmin>rct2->xmin) rct1->xmin= rct2->xmin;
00083     if(rct1->xmax<rct2->xmax) rct1->xmax= rct2->xmax;
00084     if(rct1->ymin>rct2->ymin) rct1->ymin= rct2->ymin;
00085     if(rct1->ymax<rct2->ymax) rct1->ymax= rct2->ymax;
00086 }
00087 
00088 void BLI_union_rcti(rcti *rct1, rcti *rct2)
00089 {
00090     
00091     if(rct1->xmin>rct2->xmin) rct1->xmin= rct2->xmin;
00092     if(rct1->xmax<rct2->xmax) rct1->xmax= rct2->xmax;
00093     if(rct1->ymin>rct2->ymin) rct1->ymin= rct2->ymin;
00094     if(rct1->ymax<rct2->ymax) rct1->ymax= rct2->ymax;
00095 }
00096 
00097 void BLI_init_rctf(rctf *rect, float xmin, float xmax, float ymin, float ymax)
00098 {
00099     if(xmin <= xmax) {
00100         rect->xmin= xmin;
00101         rect->xmax= xmax;
00102     }
00103     else {
00104         rect->xmax= xmin;
00105         rect->xmin= xmax;
00106     }
00107     if(ymin <= ymax) {
00108         rect->ymin= ymin;
00109         rect->ymax= ymax;
00110     }
00111     else {
00112         rect->ymax= ymin;
00113         rect->ymin= ymax;
00114     }
00115 }
00116 
00117 void BLI_init_rcti(rcti *rect, int xmin, int xmax, int ymin, int ymax)
00118 {
00119     if(xmin <= xmax) {
00120         rect->xmin= xmin;
00121         rect->xmax= xmax;
00122     }
00123     else {
00124         rect->xmax= xmin;
00125         rect->xmin= xmax;
00126     }
00127     if(ymin <= ymax) {
00128         rect->ymin= ymin;
00129         rect->ymax= ymax;
00130     }
00131     else {
00132         rect->ymax= ymin;
00133         rect->ymin= ymax;
00134     }
00135 }
00136 
00137 void BLI_translate_rcti(rcti *rect, int x, int y)
00138 {
00139     rect->xmin += x;
00140     rect->ymin += y;
00141     rect->xmax += x;
00142     rect->ymax += y;
00143 }
00144 void BLI_translate_rctf(rctf *rect, float x, float y)
00145 {
00146     rect->xmin += x;
00147     rect->ymin += y;
00148     rect->xmax += x;
00149     rect->ymax += y;
00150 }
00151 
00152 /* change width & height around the central location */
00153 void BLI_resize_rcti(rcti *rect, int x, int y)
00154 {
00155     rect->xmin= rect->xmax= (rect->xmax + rect->xmin) / 2;
00156     rect->ymin= rect->ymax= (rect->ymax + rect->ymin) / 2;
00157     rect->xmin -= x / 2;
00158     rect->ymin -= y / 2;
00159     rect->xmax= rect->xmin + x;
00160     rect->ymax= rect->ymin + y;
00161 }
00162 
00163 void BLI_resize_rctf(rctf *rect, float x, float y)
00164 {
00165     rect->xmin= rect->xmax= (rect->xmax + rect->xmin) * 0.5f;
00166     rect->ymin= rect->ymax= (rect->ymax + rect->ymin) * 0.5f;
00167     rect->xmin -= x * 0.5f;
00168     rect->ymin -= y * 0.5f;
00169     rect->xmax= rect->xmin + x;
00170     rect->ymax= rect->ymin + y;
00171 }
00172 
00173 int BLI_isect_rctf(rctf *src1, rctf *src2, rctf *dest)
00174 {
00175     float xmin, xmax;
00176     float ymin, ymax;
00177 
00178     xmin = (src1->xmin) > (src2->xmin) ? (src1->xmin) : (src2->xmin);
00179     xmax = (src1->xmax) < (src2->xmax) ? (src1->xmax) : (src2->xmax);
00180     ymin = (src1->ymin) > (src2->ymin) ? (src1->ymin) : (src2->ymin);
00181     ymax = (src1->ymax) < (src2->ymax) ? (src1->ymax) : (src2->ymax);
00182     
00183     if(xmax>=xmin && ymax>=ymin) {
00184         if(dest) {
00185             dest->xmin = xmin;
00186             dest->xmax = xmax;
00187             dest->ymin = ymin;
00188             dest->ymax = ymax;
00189         }
00190         return 1;
00191     }
00192     else {
00193         if(dest) {
00194             dest->xmin = 0;
00195             dest->xmax = 0;
00196             dest->ymin = 0;
00197             dest->ymax = 0;
00198         }
00199         return 0;
00200     }
00201 }
00202 
00203 int BLI_isect_rcti(rcti *src1, rcti *src2, rcti *dest)
00204 {
00205     int xmin, xmax;
00206     int ymin, ymax;
00207     
00208     xmin = (src1->xmin) > (src2->xmin) ? (src1->xmin) : (src2->xmin);
00209     xmax = (src1->xmax) < (src2->xmax) ? (src1->xmax) : (src2->xmax);
00210     ymin = (src1->ymin) > (src2->ymin) ? (src1->ymin) : (src2->ymin);
00211     ymax = (src1->ymax) < (src2->ymax) ? (src1->ymax) : (src2->ymax);
00212     
00213     if(xmax>=xmin && ymax>=ymin) {
00214         if(dest) {
00215             dest->xmin = xmin;
00216             dest->xmax = xmax;
00217             dest->ymin = ymin;
00218             dest->ymax = ymax;
00219         }
00220         return 1;
00221     }
00222     else {
00223         if(dest) {
00224             dest->xmin = 0;
00225             dest->xmax = 0;
00226             dest->ymin = 0;
00227             dest->ymax = 0;
00228         }
00229         return 0;
00230     }
00231 }
00232 
00233 void BLI_copy_rcti_rctf(rcti *tar, const rctf *src)
00234 {
00235     tar->xmin= floor(src->xmin + 0.5f);
00236     tar->xmax= floor((src->xmax - src->xmin) + 0.5f);
00237     tar->ymin= floor(src->ymin + 0.5f);
00238     tar->ymax= floor((src->ymax - src->ymin) + 0.5f);
00239 }
00240 
00241 void print_rctf(const char *str, rctf *rect)
00242 {
00243     printf("%s: xmin %.3f, xmax %.3f, ymin %.3f, ymax %.3f (%.3fx%.3f)\n", str, rect->xmin, rect->xmax, rect->ymin, rect->ymax, rect->xmax - rect->xmin, rect->ymax - rect->ymin);
00244 }
00245 
00246 void print_rcti(const char *str, rcti *rect)
00247 {
00248     printf("%s: xmin %d, xmax %d, ymin %d, ymax %d (%dx%d)\n", str, rect->xmin, rect->xmax, rect->ymin, rect->ymax, rect->xmax - rect->xmin, rect->ymax - rect->ymin);
00249 }