⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grid.cpp

📁 英特尔&#174 线程构建模块(英特尔&#174 TBB)是一个屡获殊荣的 C++ 运行时库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*    Copyright 2005-2007 Intel Corporation.  All Rights Reserved.    This file is part of Threading Building Blocks.    Threading Building Blocks is free software; you can redistribute it    and/or modify it under the terms of the GNU General Public License    version 2 as published by the Free Software Foundation.    Threading Building Blocks is distributed in the hope that it will be    useful, but WITHOUT ANY WARRANTY; without even the implied warranty    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with Threading Building Blocks; if not, write to the Free Software    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA    As a special exception, you may use this file as part of a free software    library without restriction.  Specifically, if other files instantiate    templates or use macros or inline functions from this file, or you compile    this file and link it with other files to produce an executable, this    file does not by itself cause the resulting executable to be covered by    the GNU General Public License.  This exception does not however    invalidate any other reasons why the executable file might be covered by    the GNU General Public License.*//*    The original source for this example is    Copyright (c) 1994, 1995, 1996, 1997  John E. Stone    All rights reserved.    Redistribution and use in source and binary forms, with or without    modification, are permitted provided that the following conditions    are met:    1. Redistributions of source code must retain the above copyright       notice, this list of conditions and the following disclaimer.    2. Redistributions in binary form must reproduce the above copyright       notice, this list of conditions and the following disclaimer in the       documentation and/or other materials provided with the distribution.    3. All advertising materials mentioning features or use of this software       must display the following acknowledgement:	 This product includes software developed by John E. Stone    4. The name of the author may not be used to endorse or promote products       derived from this software without specific prior written permission.    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE    ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF    SUCH DAMAGE.*//* * grid.c - spatial subdivision efficiency structures * * $Id: grid.cpp,v 1.2 2007/02/22 17:54:15 dpoulsen Exp $ *  */#include "machine.h"#include "types.h"#include "macros.h"#include "vector.h"#include "intersect.h"#include "util.h"#define GRID_PRIVATE#include "grid.h"#ifndef cbrt#define     cbrt(x)     ((x) > 0.0 ? pow((double)(x), 1.0/3.0) : \                          ((x) < 0.0 ? -pow((double)-(x), 1.0/3.0) : 0.0))#define     qbrt(x)     ((x) > 0.0 ? pow((double)(x), 1.0/4.0) : \                          ((x) < 0.0 ? -pow((double)-(x), 1.0/4.0) : 0.0))#endifstatic object_methods grid_methods = {  (void (*)(void *, void *))(grid_intersect),  (void (*)(void *, void *, void *, void *))(NULL),  grid_bbox,   grid_free };object * newgrid(int xsize, int ysize, int zsize, vector min, vector max) {  grid * g;  g = (grid *) rt_getmem(sizeof(grid));  memset(g, 0, sizeof(grid));    g->methods = &grid_methods;  g->id = new_objectid();  g->xsize = xsize;  g->ysize = ysize;  g->zsize = zsize;  g->min = min;  g->max = max;  VSub(&g->max, &g->min, &g->voxsize);  g->voxsize.x /= (flt) g->xsize;   g->voxsize.y /= (flt) g->ysize;   g->voxsize.z /= (flt) g->zsize;   g->cells = (objectlist **) rt_getmem(xsize*ysize*zsize*sizeof(objectlist *));  memset(g->cells, 0, xsize*ysize*zsize * sizeof(objectlist *));/* fprintf(stderr, "New grid, size: %8d %8d %8d\n", g->xsize, g->ysize, g->zsize); */  return (object *) g;}static int grid_bbox(void * obj, vector * min, vector * max) {  grid * g = (grid *) obj;   *min = g->min;  *max = g->max;  return 1;}static void grid_free(void * v) {  int i, numvoxels;  grid * g = (grid *) v;   /* loop through all voxels and free the object lists */  numvoxels = g->xsize * g->ysize * g->zsize;   for (i=0; i<numvoxels; i++) {    objectlist * lcur, * lnext;    lcur = g->cells[i];    while (lcur != NULL) {      lnext = lcur->next;      free(lcur);    }  }  /* free the grid cells */   free(g->cells);  /* free all objects on the grid object list */  free_objects(g->objects);     free(g);}static void globalbound(object ** rootlist, vector * gmin, vector * gmax) {  vector min, max;  object * cur;  if (*rootlist == NULL)  /* don't bound non-existant objects */    return;  gmin->x =  FHUGE;   gmin->y =  FHUGE;   gmin->z =  FHUGE;  gmax->x = -FHUGE;   gmax->y = -FHUGE;   gmax->z = -FHUGE;  cur=*rootlist;  while (cur != NULL)  {  /* Go! */    min.x = -FHUGE; min.y = -FHUGE; min.z = -FHUGE;    max.x =  FHUGE; max.y =  FHUGE; max.z =  FHUGE;    if (cur->methods->bbox((void *) cur, &min, &max)) {      gmin->x = MYMIN( gmin->x , min.x);      gmin->y = MYMIN( gmin->y , min.y);      gmin->z = MYMIN( gmin->z , min.z);      gmax->x = MYMAX( gmax->x , max.x);      gmax->y = MYMAX( gmax->y , max.y);      gmax->z = MYMAX( gmax->z , max.z);    }    cur=(object *)cur->nextobj;  }}static int cellbound(grid *g, gridindex *index, vector * cmin, vector * cmax) {  vector min, max, cellmin, cellmax;  objectlist * cur;  int numinbounds = 0;  cur = g->cells[index->z*g->xsize*g->ysize + index->y*g->xsize + index->x];   if (cur == NULL)  /* don't bound non-existant objects */    return 0;  cellmin.x = voxel2x(g, index->x);   cellmin.y = voxel2y(g, index->y);   cellmin.z = voxel2z(g, index->z);   cellmax.x = cellmin.x + g->voxsize.x;  cellmax.y = cellmin.y + g->voxsize.y;  cellmax.z = cellmin.z + g->voxsize.z;  cmin->x =  FHUGE;   cmin->y =  FHUGE;   cmin->z =  FHUGE;  cmax->x = -FHUGE;   cmax->y = -FHUGE;   cmax->z = -FHUGE;  while (cur != NULL)  {  /* Go! */    min.x = -FHUGE; min.y = -FHUGE; min.z = -FHUGE;    max.x =  FHUGE; max.y =  FHUGE; max.z =  FHUGE;    if (cur->obj->methods->bbox((void *) cur->obj, &min, &max)) {      if ((min.x >= cellmin.x) && (max.x <= cellmax.x) &&          (min.y >= cellmin.y) && (max.y <= cellmax.y) &&          (min.z >= cellmin.z) && (max.z <= cellmax.z)) {              cmin->x = MYMIN( cmin->x , min.x);        cmin->y = MYMIN( cmin->y , min.y);        cmin->z = MYMIN( cmin->z , min.z);        cmax->x = MYMAX( cmax->x , max.x);        cmax->y = MYMAX( cmax->y , max.y);        cmax->z = MYMAX( cmax->z , max.z);              numinbounds++;      }    }    cur=cur->next;  }   /* in case we get a 0.0 sized axis on the cell bounds, we'll */  /* use the original cell bounds */  if ((cmax->x - cmin->x) < EPSILON) {    cmax->x += EPSILON;    cmin->x -= EPSILON;  }  if ((cmax->y - cmin->y) < EPSILON) {    cmax->y += EPSILON;    cmin->y -= EPSILON;  }  if ((cmax->z - cmin->z) < EPSILON) {    cmax->z += EPSILON;    cmin->z -= EPSILON;  }  return numinbounds;}static int countobj(object * root) {  object * cur;     /* counts the number of objects on a list */  int numobj;  numobj=0;  cur=root;  while (cur != NULL) {    cur=(object *)cur->nextobj;    numobj++;  }  return numobj;}static int countobjlist(objectlist * root) {  objectlist * cur;  int numobj;  numobj=0;   cur = root;  while (cur != NULL) {    cur = cur->next;    numobj++;  }  return numobj;}int engrid_scene(object ** list) {  grid * g;  int numobj, numcbrt;  vector gmin, gmax;  gridindex index;   if (*list == NULL)    return 0;  numobj = countobj(*list);fprintf(stderr, "Scene contains %d bounded objects.\n", numobj);  if (numobj > 16) {    numcbrt = (int) cbrt(4*numobj);    globalbound(list, &gmin, &gmax);    g = (grid *) newgrid(numcbrt, numcbrt, numcbrt, gmin, gmax);    engrid_objlist(g, list);    numobj = countobj(*list);    g->nextobj = *list;    *list = (object *) g;    /* now create subgrids.. */    for (index.z=0; index.z<g->zsize; index.z++) {      for (index.y=0; index.y<g->ysize; index.y++) {        for (index.x=0; index.x<g->xsize; index.x++) {          engrid_cell(g, &index);        }      }    }   }  return 1;}void engrid_objlist(grid * g, object ** list) {  object * cur, * next, **prev;  if (*list == NULL)     return;    prev = list;   cur = *list;  while (cur != NULL) {    next = (object *)cur->nextobj;    if (engrid_object(g, cur))       *prev = next;    else       prev = (object **) &cur->nextobj;    cur = next;  } }static int engrid_cell(grid * gold, gridindex *index) {  vector gmin, gmax, gsize;  flt len;  int numobj, numcbrt, xs, ys, zs;  grid * g;  objectlist **list;  objectlist * newobj;  list = &gold->cells[index->z*gold->xsize*gold->ysize +                      index->y*gold->xsize  + index->x];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -