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