Skip to content

Commit

Permalink
indigo-utils: rendering sgroups #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Taran committed Mar 24, 2016
1 parent 43c1396 commit 2d72ff8
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 94 deletions.
135 changes: 135 additions & 0 deletions common/base_cpp/tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/****************************************************************************
* Copyright (C) 2009-2015 EPAM Systems
*
* This file is part of Indigo toolkit.
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 3 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in the
* packaging of this file.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
***************************************************************************/

#ifndef __tree_h__
#define __tree_h__

#include "base_cpp/exception.h"
#include "base_cpp/obj_array.h"

namespace indigo {

DECL_EXCEPTION(TreeError);

template <typename T> class DataTree
{
public:
DECL_TPL_ERROR(TreeError);

explicit DataTree ()
{
label = -1;
}

~DataTree ()
{
}

DataTree<T> & push ()
{
return push(-1);
}

DataTree<T> & push (int label)
{
DataTree<T> & child = _children.push();
child.label = label;
return child;
}

DataTree<T> & push (const T data)
{
DataTree<T> & child = push();
child.data = data;
return child;
}

DataTree<T> & push (int label, const T data)
{
DataTree<T> & child = push(label);
child.data = data;
return child;
}

//There is special value for root = -1
DataTree<T> & provide (int label)
{
if (label == -1) {
return *this;
}
DataTree<T> * ptr = _find_or_remove(label, false);
if (ptr != nullptr) {
return *ptr;
}
return push(label);
}

//There is special value for root = -1
DataTree<T> & insert (int label, int parent)
{
DataTree<T> & ptree = provide(parent);
return ptree.push(label);
}

bool remove (int label)
{
return _find_or_remove(label, true) != nullptr;
}

bool find (int label)
{
return _find_or_remove(label, false) != nullptr;
}

ObjArray<DataTree<T>> & children() {
return _children;
}

int label;
T data;

protected:
ObjArray<DataTree<T>> _children;

private:
DataTree(const DataTree<T> &); // no implicit copy
DataTree<T>& operator= (const DataTree<T>& other); // no copy constructor

DataTree<T> * _find_or_remove (int label, bool remove)
{
for (int i = 0; i < _children.size(); i++) {
DataTree<T> & child = _children[i];
if (child.label == label) {
if (remove) {
_children.remove(i);
}
return &child;
} else {
DataTree<T> * result = child._find_or_remove(label, remove);
if (result != nullptr) {
return result;
}
}
}
return nullptr;
}
};

struct Empty {};

typedef DataTree<Empty> Tree;

}

#endif
53 changes: 53 additions & 0 deletions common/math/algebra.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,59 @@ struct Vec2f
DLLEXPORT static Vec2f get_circle_center(Vec2f a, Vec2f b, Vec2f c);
};

struct Rect2f {

explicit Rect2f () {}

Rect2f (Vec2f a, Vec2f b)
{
_leftBottom = a;
_leftBottom.min(b);
_rightTop = a;
_rightTop.max(b);
}

Rect2f (Rect2f a, Rect2f b)
{
_leftBottom = a._leftBottom;
_leftBottom.min(b._leftBottom);
_rightTop = a._rightTop;
_rightTop.max(b._rightTop);
}

inline void copy (Rect2f &other)
{
_leftBottom = other._leftBottom;
_rightTop = other._rightTop;
}

inline float left() const { return _leftBottom.x; }
inline float right() const { return _rightTop.x; }
inline float bottom() const { return _leftBottom.y; }
inline float top() const { return _rightTop.y; }

inline float middleX() const { return (_leftBottom.x + _rightTop.x) / 2; }
inline float middleY() const { return (_leftBottom.y + _rightTop.y) / 2; }

inline Vec2f leftBottom() const { return _leftBottom; }
inline Vec2f rightTop() const { return _rightTop; }

inline Vec2f leftTop() const { return Vec2f(left(), top()); }
inline Vec2f rightBottom() const { return Vec2f(right(), bottom()); }

inline Vec2f leftMiddle() const { return Vec2f(left(), middleY()); }
inline Vec2f rightMiddle() const { return Vec2f(right(), middleY()); }

inline Vec2f bottomMiddle() const { return Vec2f(middleX(), bottom()); }
inline Vec2f topMiddle() const { return Vec2f(middleX(), top()); }

inline Vec2f center() const { return Vec2f(middleX(), middleY()); }

protected:
Vec2f _leftBottom;
Vec2f _rightTop;
};

struct Vec3f
{
Vec3f () : x(0), y(0), z(0) {}
Expand Down
1 change: 0 additions & 1 deletion molecule/molecule_attachments_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#ifndef __molecule_attachments_search__
#define __molecule_attachments_search__

#include "assert.h"
#include "algorithm"

#include "base_cpp/array.h"
Expand Down
1 change: 0 additions & 1 deletion molecule/molecule_sgroups.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ class DLLEXPORT MoleculeSGroups
bool _cmpIndices (Array<int> &t_inds, Array<int> &q_inds);
};


}

#ifdef _WIN32
Expand Down
1 change: 1 addition & 0 deletions molecule/src/molecule_attachments_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
***************************************************************************/

#include "assert.h"
#include "base_cpp/array.h"
#include "molecule/molecule_attachments_search.h"

Expand Down
43 changes: 42 additions & 1 deletion render2d/render_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define __render_internal_h__

#include "render_common.h"
#include "base_cpp/tree.h"

namespace indigo {

Expand Down Expand Up @@ -76,8 +77,11 @@ class MoleculeRenderInternal {
void _placeBrackets(Sgroup& sg, const Array<int>& atoms);
void _positionIndex(Sgroup& sg, int ti, bool lower);
void _loadBracketsAuto(const SGroup& group, Sgroup& sg);
void _initSGroups();

void _prepareSGroups();
void _initSGroups(Tree& sgroups, Vec2f extra);
void _initSGroups();

void _findAnglesOverPi();
void _renderBondIds();
void _renderAtomIds();
Expand Down Expand Up @@ -137,6 +141,43 @@ class MoleculeRenderInternal {
void _bondAny (BondDescr& bd, const BondEnd& be1, const BondEnd& be2);
int _parseColorString (Scanner& str, float& r, float& g, float& b);

void _cloneAndFillMappings();

//TODO: remove dublicate with _placeBrackets(..)
inline Rect2f _bound(Array<int>& atoms) const {
const int n = atoms.size();
Array<Vec2f> points;
points.resize(n);
for (int i = 0; i < n; i++) {
points[i] = _ad(atoms[i]).pos;
}
return _bound(points, 0, n-1);
}

Rect2f _bound(Array<Vec2f>& points, int l, int r) const {
if (r == l || r == l + 1) {
return Rect2f(points[l], points[r]);
}
int m = (l + r) / 2;
return Rect2f(
_bound(points, l, m),
_bound(points, m+1, r)
);
}

inline Vec2f _firstPosition(Array<int>& atoms) {
return _ad(atoms[0]).pos;
}

//TODO: move into Vec2f?
const Vec2f ILLEGAL_POINT = Vec2f(nanf(""), nanf(""));
inline bool _isIllegal(Vec2f point) {
return _isNaN(point.x) && _isNaN(point.y);
}
inline bool _isNaN(float x) {
return x != x;
}

// local
void* _hdc;
BaseMolecule* _mol;
Expand Down
2 changes: 1 addition & 1 deletion render2d/render_item_fragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RenderItemFragment : public RenderItemBase {

virtual void estimateSize ();
virtual void setObjScale (float scale) {
_scaleFactor = scale;
_scaleFactor = scale;
}
virtual void init ();
virtual void render ();
Expand Down
3 changes: 2 additions & 1 deletion render2d/src/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ float Render::_getObjScale (int item)
} else {
avgBondLength = _factory.getItem(item).getTotalClosestAtomDistance() / atomCount;
}
if (avgBondLength < 1e-4)
if (avgBondLength < 1e-4) {
avgBondLength = 1.0f;
}
float objScale = 1 / avgBondLength;
return objScale;
}
Expand Down
Loading

0 comments on commit 2d72ff8

Please sign in to comment.