Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 533 lines (402 sloc) 11.774 kb
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
#ifndef __VOXEL_GRID_GROUP_H__
#define __VOXEL_GRID_GROUP_H__


#include <ImathBox.h>
#include <ImathVec.h>
#include <ImathColor.h>
#include <QString>
#include <QVector>
#include <QSharedData>
#include <QExplicitlySharedDataPointer>

#include "GameVoxelGrid.h"
#include "RayWalk.h"


//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//


typedef Imath::Color4f SproxelColor;
typedef unsigned char SproxelIndex;


typedef GameVoxelGrid<SproxelColor> RgbVoxelGrid;
typedef GameVoxelGrid<SproxelIndex> IndVoxelGrid;


//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//


inline float color_diff(const SproxelColor &a, const SproxelColor &b)
{
  SproxelColor d=a-b; d*=d;
  return d.r+d.g+d.b+d.a;
}


class ColorPalette : public QSharedData
{
protected:
  std::vector<SproxelColor> m_colors;
  QString m_name;

public:

  ColorPalette() {}

  template<class I> ColorPalette(I first, I last) : m_colors(first, last) {}


  QString name() const { return m_name; }
  void setName(const QString n) { m_name=n; }

  int numColors() const { return m_colors.size(); }

  void resize(int new_size)
  {
    if (new_size<0) new_size=0;
    m_colors.resize(new_size, SproxelColor(0, 0, 0, 0));
  }

  SproxelColor color(int i) const
  {
    if (i<0 || i>=(int)m_colors.size()) return SproxelColor(0, 0, 0, 0);
    return m_colors[i];
  }

  void setColor(int i, const SproxelColor &c)
  {
    if (i<0) return;
    if (i>=(int)m_colors.size()) resize(i+1);
    m_colors[i]=c;
  }

  int bestMatch(const SproxelColor &c) const
  {
    int bi=-1;
    float bd=FLT_MAX;

    for (size_t i=0; i<m_colors.size(); ++i)
    {
      float d=color_diff(m_colors[i], c);
      if (d<bd) { bd=d; bi=i; }
    }

    return bi;
  }
};


typedef QExplicitlySharedDataPointer<ColorPalette> ColorPalettePtr;


//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//


typedef QExplicitlySharedDataPointer<class VoxelGridLayer> VoxelGridLayerPtr;


class VoxelGridLayer : public QSharedData
{
protected:
  RgbVoxelGrid *m_rgb;
  IndVoxelGrid *m_ind;
  ColorPalettePtr m_palette;

  Imath::V3i m_offset;
  QString m_name;
  bool m_visible;

  void init()
  {
    m_rgb=NULL;
    m_ind=NULL;
    m_palette=NULL;
    m_offset=Imath::V3i(0);
    m_name="layer";
    m_visible=true;
  }

public:

  enum DataType { TYPE_RGB, TYPE_IND };


  VoxelGridLayer() { init(); }

  VoxelGridLayer(const RgbVoxelGrid &grid, const Imath::V3i ofs=Imath::V3i(0))
  {
    init();
    m_rgb=new RgbVoxelGrid(grid);
    m_offset=ofs;
  }

  VoxelGridLayer(const IndVoxelGrid &grid, ColorPalette *pal=NULL, const Imath::V3i ofs=Imath::V3i(0))
  {
    init();
    m_ind=new IndVoxelGrid(grid);
    m_palette=pal;
    m_offset=ofs;
  }

  void clear()
  {
    if (m_rgb) { delete m_rgb; m_rgb=NULL; }
    if (m_ind) { delete m_ind; m_ind=NULL; }
    init();
  }

  ~VoxelGridLayer()
  {
    clear();
  }

  VoxelGridLayer(const VoxelGridLayer &from) :
    m_rgb (from.m_rgb ),
    m_ind (from.m_ind ),
    m_palette(from.m_palette),
    m_offset (from.m_offset ),
    m_name (from.m_name ),
    m_visible(from.m_visible)
  {
    if (m_rgb) m_rgb=new RgbVoxelGrid(*m_rgb);
    if (m_ind) m_ind=new IndVoxelGrid(*m_ind);
  }

  VoxelGridLayer& operator = (const VoxelGridLayer &from)
  {
    if (&from==this) return *this;

    clear();

    if (from.m_rgb) m_rgb=new RgbVoxelGrid(*from.m_rgb);
    if (from.m_ind) m_ind=new IndVoxelGrid(*from.m_ind);
    m_palette=from.m_palette;
    m_offset =from.m_offset ;
    m_name =from.m_name ;
    m_visible=from.m_visible;

    return *this;
  }

  const Imath::V3i& offset() const { return m_offset; }
  void setOffset(const Imath::V3i &o) { m_offset=o; }

  bool isVisible() const { return m_visible; }
  void setVisible(bool v) { m_visible=v; }

  QString name() const { return m_name; }
  void setName(const QString n) { m_name=n; }

  ColorPalettePtr palette() const { return m_palette; }
  void setPalette(ColorPalettePtr p) { m_palette=p; }

  Imath::V3i size() const
  {
    if (m_ind) return m_ind->cellDimensions();
    if (m_rgb) return m_rgb->cellDimensions();
    return Imath::V3i(0);
  }

  Imath::Box3i bounds() const
  {
    return Imath::Box3i(m_offset, m_offset+size()-Imath::V3i(1));
  }

  void resize(const Imath::Box3i &new_box)
  {
    Q_ASSERT(!new_box.isEmpty());

    // expand grid and adjust offset to match new box
    Imath::Box3i curBox=bounds();

    if (m_ind)
    {
      m_ind->resize(new_box.size()+Imath::V3i(1), new_box.min-curBox.min, 0);
      m_offset=new_box.min;
    }
    else if (m_rgb)
    {
      m_rgb->resize(new_box.size()+Imath::V3i(1), new_box.min-curBox.min, SproxelColor(0, 0, 0, 0));
      m_offset=new_box.min;
    }
    else if (!new_box.isEmpty())
    {
      // no grids yet - create a new one
      if (m_palette)
      {
        m_ind=new IndVoxelGrid(new_box.size()+Imath::V3i(1));
        m_ind->setAll(0);
      }
      else
      {
        m_rgb=new RgbVoxelGrid(new_box.size()+Imath::V3i(1));
        m_rgb->setAll(SproxelColor(0, 0, 0, 0));
      }
      m_offset=new_box.min;
    }
  }

  int getInd(const Imath::V3i &at) const
  {
    if (!m_ind) return -1;
    if (!bounds().intersects(at)) return -1;
    return m_ind->get(at-m_offset);
  }

  SproxelColor getColor(const Imath::V3i &at) const
  {
    if (!bounds().intersects(at)) return SproxelColor(0, 0, 0, 0);
    if (m_ind && m_palette) return m_palette->color(m_ind->get(at-m_offset));
    if (m_rgb) return m_rgb->get(at-m_offset);
    return SproxelColor(0, 0, 0, 0);
  }

  void set(const Imath::V3i &at, const SproxelColor &color, int index=-1)
  {
    // expand grid to include target voxel
    Imath::Box3i box=bounds();
    if (!box.intersects(at))
    {
        box.extendBy(at);
        resize(box);
    }

    if (m_ind)
    {
      if (index<0)
      {
        if (m_palette)
        {
          //== TODO: could expand palette instead, up to some maximum number of colors
          index=m_palette->bestMatch(color);
        }
        else
          index=0;
      }

      // set voxel index
      m_ind->set(at-m_offset, index);
    }
    else if (m_rgb)
    {
      // set voxel color
      m_rgb->set(at-m_offset, color);
    }
  }

  DataType dataType() const { return m_ind ? TYPE_IND : TYPE_RGB; }
  bool isIndexed() const { return m_ind!=NULL; }

  class QImage makeQImage() const;

  static VoxelGridLayerPtr fromQImage(class QImage, ColorPalettePtr);
};


//ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ//


class VoxelGridGroup : public QSharedData
{
private:
  Imath::M44d m_transform;
  QVector<VoxelGridLayerPtr> m_layers;
  int m_curLayer;
  QString m_name;

public:

  VoxelGridGroup(VoxelGridLayerPtr layer=VoxelGridLayerPtr()) : m_transform(), m_curLayer(-1)
  {
    if (layer)
    {
      m_layers.push_back(layer);
      m_curLayer=0;
    }
  }

  VoxelGridGroup(const Imath::V3i &size, ColorPalettePtr palette) : m_transform()
  {
    VoxelGridLayerPtr layer(new VoxelGridLayer());
    layer->setPalette(palette);
    layer->resize(Imath::Box3i(Imath::V3i(0), size-Imath::V3i(1)));
    layer->setName("main layer");

    m_layers.push_back(layer);
    m_curLayer=0;
  }

  VoxelGridGroup(const VoxelGridGroup &from)
  {
    m_transform=from.m_transform;
    m_curLayer =from.m_curLayer ;
    m_name =from.m_name ;

    m_layers.reserve(from.m_layers.size());
    for (int i=0; i<from.m_layers.size(); ++i)
      m_layers.push_back(VoxelGridLayerPtr(new VoxelGridLayer(*from.m_layers[i])));
  }

  VoxelGridGroup& operator = (const VoxelGridGroup &from)
  {
    if (&from==this) return *this;

    clear();

    m_transform=from.m_transform;
    m_curLayer =from.m_curLayer ;

    m_layers.reserve(from.m_layers.size());
    for (int i=0; i<from.m_layers.size(); ++i)
      m_layers.push_back(VoxelGridLayerPtr(new VoxelGridLayer(*from.m_layers[i])));

    return *this;
  }

  void clear()
  {
    m_transform.makeIdentity();
    m_curLayer=-1;
    m_layers.clear();
  }

  ~VoxelGridGroup() { clear(); }

  // General accessors
  const Imath::M44d& transform() const { return m_transform; }
  void setTransform(const Imath::M44d& m) { m_transform = m; }

  QString name() const { return m_name; }
  void setName(QString n) { m_name=n; }


  int curLayerIndex() const { return m_curLayer; }

  VoxelGridLayerPtr curLayer() const
  {
    if (m_curLayer<0) return VoxelGridLayerPtr(NULL);
    return m_layers[m_curLayer];
  }

  void setCurLayer(int index)
  {
    if (index<0 || index>=(int)m_layers.size()) index=-1;
    m_curLayer=index;
  }


  Imath::Box3i bounds() const
  {
    Imath::Box3i bbox;
    for (int i=0; i<m_layers.size(); ++i) bbox.extendBy(m_layers[i]->bounds());
    return bbox;
  }


  // Layer accessors
  int numLayers() const { return m_layers.size(); }

  VoxelGridLayerPtr layer(int i) const
  {
    if (i<0 || i>=(int)m_layers.size()) return VoxelGridLayerPtr(NULL);
    return m_layers[i];
  }

  VoxelGridLayerPtr insertLayerAbove(int i, VoxelGridLayerPtr layer=VoxelGridLayerPtr())
  {
    if (!layer) layer=new VoxelGridLayer();
    m_layers.insert(m_layers.begin()+i, VoxelGridLayerPtr(layer));
    if (m_curLayer>=i) ++m_curLayer;
    return VoxelGridLayerPtr(layer);
  }

  VoxelGridLayerPtr removeLayer(int i)
  {
    if (i<0 || i>=(int)m_layers.size()) return VoxelGridLayerPtr(NULL);
    VoxelGridLayerPtr layer=m_layers[i];
    m_layers.erase(m_layers.begin()+i);

    if (m_curLayer>i) --m_curLayer;
    if (m_curLayer>=(int)m_layers.size()) m_curLayer=m_layers.size()-1;

    return layer;
  }

  void deleteLayer(int i)
  {
    removeLayer(i);
  }

  bool layerVisible(int i)
  {
    if (i<0 || i>=(int)m_layers.size()) return false;
    return m_layers[i]->isVisible();
  }

  QString layerName(int i)
  {
    if (i<0 || i>=(int)m_layers.size()) return "";
    return m_layers[i]->name();
  }


  bool hasPalette(ColorPalettePtr pal) const
  {
    for (int i=0; i<m_layers.size(); ++i)
      if (m_layers[i]->palette()==pal) return true;

    return false;
  }


  VoxelGridLayerPtr bakeLayers() const;


  Imath::V3d voxelCenter(const Imath::V3i& v) const
  {
    return Imath::V3d(
      (double)v.x + 0.5,
      (double)v.y + 0.5,
      (double)v.z + 0.5
    );
  }

  const Imath::M44d voxelTransform(const Imath::V3i& v) const
  {
    Imath::M44d vMat;
    vMat.setTranslation(voxelCenter(v));
    return vMat * m_transform;
  }

  const Imath::Box3d worldBounds() const
  {
    const Imath::Box3i box=bounds();
    const Imath::Box3d retBox(box.min, box.max+Imath::V3i(1));

    // (ImathBoxAlgo) This properly computes the world bounding box
    return Imath::transform(retBox, m_transform);
  }


  int getInd(const Imath::V3i &at) const
  {
    int result=0;

    for (int i=0; i<m_layers.size(); ++i)
    {
      int ind=m_layers[i]->getInd(at);
      if (ind>0) { result=ind; break; }
    }

    return result;
  }


  SproxelColor get(const Imath::V3i &at) const
  {
    SproxelColor result(0, 0, 0, 0);

    for (int i=0; i<m_layers.size(); ++i)
    {
      SproxelColor c=m_layers[i]->getColor(at);
      if (c.a!=0) { result=c; break; }
    }

    return result;
  }


  void set(const Imath::V3i &at, const SproxelColor &color, int index=-1)
  {
    VoxelGridLayerPtr layer=curLayer();
    if (layer) layer->set(at, color, index);
  }


  std::vector<Imath::V3i> rayIntersection(const Imath::Line3d &worldRay)
  {
    // Transform the ray into voxel space
    Imath::Line3d localRay = worldRay * m_transform.inverse();

    return walk_ray(localRay, bounds());
  }

};


typedef QExplicitlySharedDataPointer<VoxelGridGroup> VoxelGridGroupPtr;


#endif
Something went wrong with that request. Please try again.