Commit 919a5923 authored by Kirill Terekhov's avatar Kirill Terekhov

Fix and updates

Fix const-correctness in class matrix

Display references as segments and variable as field in OldDrawGrid example. Separate OldDrawGrid source into files.
parent a3e1505b
......@@ -4,7 +4,11 @@ set(SOURCE main.cpp
clipboard.cpp
rotate.h
my_glut.h
clipboard.h)
clipboard.h
color.h
coord.h
coord.cpp
octree.h)
find_package(OpenGL)
find_package(GLUT)
......
#ifndef _COLOR_H
#define _COLOR_H
#include "my_glut.h"
#include <iostream>
struct color_t
{
float c[4];
color_t() { memset(c, 0, sizeof(float)* 4); }
color_t(float r, float g, float b)
{
c[0] = r;
c[1] = g;
c[2] = b;
c[3] = 1.0;
}
color_t(float r, float g, float b, float a)
{
c[0] = r;
c[1] = g;
c[2] = b;
c[3] = a;
}
color_t(const color_t & other) { memcpy(c, other.c, sizeof(float)* 4); }
color_t & operator =(color_t const & other)
{
memmove(c, other.c, sizeof(float)* 4);
return *this;
}
void set_color() const { glColor4fv(c); }
float & r() { return c[0]; }
float & g() { return c[1]; }
float & b() { return c[2]; }
float & a() { return c[3]; }
float r() const { return c[0]; }
float g() const { return c[1]; }
float b() const { return c[2]; }
float a() const { return c[3]; }
color_t operator *(float mult)const { return color_t(c[0] * mult, c[1] * mult, c[2] * mult, c[3] * mult); }
color_t operator +(color_t other)const{ return color_t(c[0] + other.c[0], c[1] + other.c[1], c[2] + other.c[2], c[3] + other.c[3]); }
color_t operator -(color_t other)const { return color_t(c[0] - other.c[0], c[1] - other.c[1], c[2] - other.c[2], other.c[3]); }
std::string svg_rgb() const
{
std::stringstream out;
out << "rgb(" << floor(r() * 255) << ", " << floor(g() * 255) << ", " << floor(b() * 255) << ")";
return out.str();
}
std::string svg_rgba_fill() const
{
std::stringstream out;
out << "fill=\"" << svg_rgb() << "\" fill-opacity=\"" << a() << "\"";
return out.str();
}
std::string svg_rgba_stroke() const
{
std::stringstream out;
out << "stroke=\"" << svg_rgb() << "\" stroke-opacity=\"" << a() << "\"";
return out.str();
}
};
#endif
\ No newline at end of file
#include "coord.h"
double abs(const coord & p)
{
return sqrt(p^p);
}
void get_matrix(const coord & a, const coord & b, double matrix[16])
{
double d;
coord z = (b - a) / sqrt((b - a) ^ (b - a));
coord y;
coord x;
y = coord(z[1], -z[2], 0);
d = sqrt(y^y);
if (d < 1e-5)
{
y = coord(-z[2], 0, z[0]);
d = sqrt(y^y);
}
y = y / d;
x = y*z;
x = x / sqrt(x^x);
y = x*z;
matrix[0] = x[0];
matrix[1] = x[1];
matrix[2] = x[2];
matrix[3] = 0;
matrix[4] = y[0];
matrix[5] = y[1];
matrix[6] = y[2];
matrix[7] = 0;
matrix[8] = z[0];
matrix[9] = z[1];
matrix[10] = z[2];
matrix[11] = 0;
matrix[12] = 0;
matrix[13] = 0;
matrix[14] = 0;
matrix[15] = 1;
}
\ No newline at end of file
#ifndef _COORD_H
#define _COORD_H
#include <cmath>
class coord
{
double p[3];
public:
coord() { p[0] = p[1] = p[2] = 0; }
coord(double xyz[3]) { p[0] = xyz[0]; p[1] = xyz[1]; p[2] = xyz[2]; }
coord(double x, double y, double z) { p[0] = x; p[1] = y; p[2] = z; }
coord(const coord & other) { p[0] = other.p[0]; p[1] = other.p[1]; p[2] = other.p[2]; }
coord & operator = (coord const & other) { p[0] = other.p[0]; p[1] = other.p[1]; p[2] = other.p[2]; return *this; }
coord & operator +=(const coord & other) { p[0] += other.p[0]; p[1] += other.p[1]; p[2] += other.p[2]; return *this; }
coord & operator -=(const coord & other) { p[0] -= other.p[0]; p[1] -= other.p[1]; p[2] -= other.p[2]; return *this; }
coord & operator *=(const coord & other)
{
double tmp[3] = { p[1] * other.p[2] - p[2] * other.p[1], p[2] * other.p[0] - p[0] * other.p[2], p[0] * other.p[1] - p[1] * other.p[0] };
p[0] = tmp[0]; p[1] = tmp[1]; p[2] = tmp[2];
return *this;
}
coord & operator *=(double other) { p[0] *= other; p[1] *= other; p[2] *= other; return *this; }
coord & operator /=(double other) { p[0] /= other; p[1] /= other; p[2] /= other; return *this; }
coord operator -(const coord & other) const { return coord(p[0] - other.p[0], p[1] - other.p[1], p[2] - other.p[2]); }
coord operator +(const coord & other) const { return coord(p[0] + other.p[0], p[1] + other.p[1], p[2] + other.p[2]); }
coord operator *(const coord & other) const { return coord(p[1] * other.p[2] - p[2] * other.p[1], p[2] * other.p[0] - p[0] * other.p[2], p[0] * other.p[1] - p[1] * other.p[0]); }
coord operator /(double other) const { return coord(p[0] / other, p[1] / other, p[2] / other); }
coord operator *(double other) const { return coord(p[0] * other, p[1] * other, p[2] * other); }
double operator ^(const coord & other) const { return p[0] * other.p[0] + p[1] * other.p[1] + p[2] * other.p[2]; }
~coord() {}
double length() const { return sqrt((*this) ^ (*this)); }
double & operator [](int i) { return p[i]; }
double operator [](int i) const { return p[i]; }
double * data() { return p; }
const double * data()const { return p; }
};
double abs(const coord & p);
void get_matrix(const coord & a, const coord & b, double matrix[16]);
#endif
\ No newline at end of file
This diff is collapsed.
#ifndef _OCTREE_H
#define _OCTREE_H
#include "inmost.h"
namespace INMOST
{
class Octree : public ElementSet
{
Tag save_center_tag;
bool save_quad_tree;
void SubConstruct(const Tag & child_tag, const Tag & center_tag, HandleType * cells, HandleType * temp, int size, bool quad_tree)
{
Storage::real_array center = RealArray(center_tag);
//create 8 nodes
Storage::real cell_center[3];
int offsets[8], sizes[8];
int dims = 3 - (quad_tree ? 1 : 0);
int numchildren = (1 << dims);
for (int k = 0; k < numchildren; ++k)
{
offsets[k] = 0;
sizes[k] = 0;
}
for (int r = 0; r < size; ++r)
{
Element c = Element(GetMeshLink(), cells[r]);
c->Centroid(cell_center);
int child_num = 0;
for (int k = 0; k < dims; ++k)
{
if (cell_center[k] > center[k])
{
int m = 1 << k;
child_num += m;
}
}
c->IntegerDF(child_tag) = child_num;
sizes[child_num]++;
}
for (int k = 1; k < numchildren; ++k)
{
offsets[k] = offsets[k - 1] + sizes[k - 1];
}
for (int k = 0; k < numchildren; ++k)
{
std::stringstream name;
name << GetName() << "chld" << k;
ElementSet child = GetMeshLink()->CreateSetUnique(name.str()).first;
Storage::real_array child_center = child->RealArray(center_tag);
for (int r = 0; r < dims; ++r)
{
int l = 1 << r;
int m = k & l;
child_center[r] = center[r] + ((m ? 1.0 : -1.0) * center[r + 3] * 0.25);
child_center[r + 3] = center[r + 3] * 0.5;
}
int m = 0;
for (int r = 0; r < size; ++r)
{
Element c = Element(GetMeshLink(), cells[r]);
int q = c->IntegerDF(child_tag);
if (q == k) (temp + offsets[k])[m++] = cells[r];
}
AddChild(child);
if (sizes[k] <= 16 && sizes[k] > 0)
child->PutElements(temp + offsets[k], sizes[k]);
}
// cells array is not needed anymore
ElementSet child = GetChild();
for (int k = 0; k < numchildren; ++k)
{
if (sizes[k] > 16)
Octree(child).SubConstruct(child_tag, center_tag, temp + offsets[k], cells + offsets[k], sizes[k], quad_tree);
child = child->GetSibling();
}
}
Cell SubFindCell(const Tag & center_tag, Storage::real pnt[3], bool quad_tree) const
{
if (HaveChild())
{
Storage::real_array center = RealArray(center_tag);
int child_num = 0, q;
int dims = 3 - (quad_tree ? 1 : 0);
for (int k = 0; k < dims; ++k)
{
if (pnt[k] > center[k])
child_num += (1 << k);
}
q = 0;
ElementSet set = GetChild();
while (q != child_num) { set = set->GetSibling(); q++; }
return Octree(set).SubFindCell(center_tag, pnt, quad_tree);
}
else
{
HandleType * cells = getHandles();
int ncells = (int)nbHandles();
Node closest = InvalidNode();
Storage::real mindist = 1.0e20, dist;
for (int k = 0; k < ncells; ++k)
{
Node c = Node(GetMeshLink(), cells[k]);
Storage::real_array cnt = c->Coords();
dist = sqrt((cnt[0] - pnt[0])*(cnt[0] - pnt[0]) + (cnt[1] - pnt[1])*(cnt[1] - pnt[1]) + (cnt[2] - pnt[2])*(cnt[2] - pnt[2]));
if (mindist > dist)
{
mindist = dist;
closest = c;
}
}
if (closest.isValid())
{
ElementArray<Cell> cells = closest->getCells();
for (ElementArray<Cell>::iterator c = cells.begin(); c != cells.end(); ++c)
if (c->Inside(pnt)) return c->self();
}
return InvalidCell();
}
}
bool Inside(const Storage::real_array & center, Storage::real pnt[3], bool quad_tree) const
{
bool inside = true;
int dims = 3 - (quad_tree ? 1 : 0);
for (int i = 0; i < dims; ++i)
inside &= (pnt[i] >= center[i] - center[3 + i] * 0.5 && pnt[i] <= center[i] + center[3 + i] * 0.5);
return inside;
}
Node SubFindNode(const Tag & center_tag, Storage::real pnt[3], bool quad_tree) const
{
if (HaveChild())
{
Storage::real_array center = RealArray(center_tag);
if (!Inside(center, pnt, quad_tree)) return InvalidNode();
int child_num = 0, q;
int dims = 3 - (quad_tree ? 1 : 0);
for (int k = 0; k < dims; ++k)
{
if (pnt[k] > center[k])
child_num += (1 << k);
}
q = 0;
ElementSet set = GetChild();
while (q != child_num) { set = set->GetSibling(); q++; }
return Octree(set).SubFindNode(center_tag, pnt, quad_tree);
}
else
{
HandleType * cells = getHandles();
int ncells = (int)nbHandles();
Node closest = InvalidNode();
Storage::real mindist = 1.0e20, dist;
for (int k = 0; k < ncells; ++k)
{
Node c = Node(GetMeshLink(), cells[k]);
Storage::real_array cnt = c->Coords();
dist = sqrt((cnt[0] - pnt[0])*(cnt[0] - pnt[0]) + (cnt[1] - pnt[1])*(cnt[1] - pnt[1]) + (cnt[2] - pnt[2])*(cnt[2] - pnt[2]));
if (mindist > dist)
{
mindist = dist;
closest = c;
}
}
return closest;
}
}
void SubDestroy()
{
if (HaveChild())
{
ElementSet set = GetChild(), next;
while (set->isValid())
{
next = set->GetSibling();
Octree(set).SubDestroy();
set = next;
}
}
DeleteSet();
handle = InvalidHandle();
handle_link = NULL;
}
public:
Octree() : ElementSet(InvalidElementSet()) {}
Octree(const Octree & other) : ElementSet(other) {}
Octree(const ElementSet & eset) : ElementSet(eset) {}
void Construct(ElementType elem, bool quad_tree = false)
{
save_quad_tree = quad_tree;
int dims = 3 - (quad_tree ? 1 : 0);
Tag child_tag = GetMeshLink()->CreateTag("OCTREE_CHILD_NUM_" + GetName(), DATA_INTEGER, elem, NONE, 1);
save_center_tag = GetMeshLink()->CreateTag("OCTREE_CENTER_" + GetName(), DATA_REAL, ESET, ESET, 6);
Storage::real bounds[3][2];
for (int k = 0; k < dims; ++k)
{
bounds[k][0] = 1.0e20;
bounds[k][1] = -1.0e20;
}
//calculate bounds
for (Mesh::iteratorNode node = GetMeshLink()->BeginNode(); node != GetMeshLink()->EndNode(); ++node)
{
Storage::real_array coord = node->Coords();
for (int k = 0; k < dims; ++k)
{
if (coord[k] < bounds[k][0]) bounds[k][0] = coord[k];
if (coord[k] > bounds[k][1]) bounds[k][1] = coord[k];
}
}
Storage::real_array center_data = RealArray(save_center_tag);
for (int k = 0; k < dims; ++k)
{
center_data[k] = (bounds[k][0] + bounds[k][1])*0.5; //central position
center_data[k + 3] = bounds[k][1] - bounds[k][0]; //length
}
//copy cells
int size = GetMeshLink()->NumberOf(elem), k = 0;
HandleType * cells = new HandleType[size * 2];
HandleType * temp = cells + size;
for (Mesh::iteratorElement cell = GetMeshLink()->BeginElement(elem); cell != GetMeshLink()->EndElement(); ++cell)
cells[k++] = *cell;
SubConstruct(child_tag, save_center_tag, cells, temp, size, quad_tree);
GetMeshLink()->DeleteTag(child_tag);
}
Cell FindCell(Storage::real pnt[3]) const
{
return SubFindCell(save_center_tag, pnt, save_quad_tree);
}
Node FindNode(Storage::real pnt[3]) const
{
return SubFindNode(save_center_tag, pnt, save_quad_tree);
}
void Destroy()
{
if (save_center_tag.isValid())
GetMeshLink()->DeleteTag(save_center_tag);
SubDestroy();
}
~Octree() { }
};
}
#endif
\ No newline at end of file
......@@ -1142,11 +1142,11 @@ namespace INMOST
/// @param B Matrix to be concatenated to current matrix.
/// @return Result of concatenation of current matrix and parameter.
/// @see Matrix::ConcatRows
Matrix ConcatCols(const Matrix & B)
Matrix ConcatCols(const Matrix & B) const
{
assert(Rows() == B.Rows());
Matrix ret(Rows(),Cols()+B.Cols());
Matrix & A = *this;
const Matrix & A = *this;
for(enumerator i = 0; i < Rows(); ++i)
{
for(enumerator j = 0; j < Cols(); ++j)
......@@ -1162,11 +1162,11 @@ namespace INMOST
/// @param B Matrix to be concatenated to current matrix.
/// @return Result of concatenation of current matrix and parameter.
/// @see Matrix::ConcatCols
Matrix ConcatRows(const Matrix & B)
Matrix ConcatRows(const Matrix & B) const
{
assert(Cols() == B.Cols());
Matrix ret(Rows()+B.Rows(),Cols());
Matrix & A = *this;
const Matrix & A = *this;
for(enumerator i = 0; i < Rows(); ++i)
{
for(enumerator j = 0; j < Cols(); ++j)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment