Skip to content

Commit

Permalink
indigo-utils: rendering sgroups #8, depict version + inchi parameters #…
Browse files Browse the repository at this point in the history
…18, charged pseudos rendering #41
  • Loading branch information
Kirill Taran committed Apr 19, 2016
1 parent ae90b6c commit 0c72126
Show file tree
Hide file tree
Showing 10 changed files with 414 additions and 249 deletions.
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_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
8 changes: 5 additions & 3 deletions molecule/src/base_molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ void BaseMolecule::mergeSGroupsWithSubmolecule (BaseMolecule &mol, Array<int> &m
SGroup &supersg = mol.sgroups.getSGroup(i);
int idx = sgroups.addSGroup(supersg.sgroup_type);
SGroup &sg = sgroups.getSGroup(idx);
if (_mergeSGroupWithSubmolecule(sg, supersg, mol, mapping, edge_mapping))

//TODO: check
_mergeSGroupWithSubmolecule(sg, supersg, mol, mapping, edge_mapping);
{
if (sg.sgroup_type == SGroup::SG_TYPE_DAT)
{
Expand Down Expand Up @@ -193,8 +195,6 @@ void BaseMolecule::mergeSGroupsWithSubmolecule (BaseMolecule &mol, Array<int> &m
mg.parent_atoms.push(mapping[supermg.parent_atoms[j]]);
}
}
else
sgroups.remove(idx);
}
}

Expand Down Expand Up @@ -1514,6 +1514,8 @@ bool BaseMolecule::_mergeSGroupWithSubmolecule (SGroup &sgroup, SGroup &super, B
int i;
bool merged = false;

sgroup.parent_group = super.parent_group;

sgroup.sgroup_subtype = super.sgroup_subtype;

sgroup.brackets.copy(super.brackets);
Expand Down
51 changes: 37 additions & 14 deletions molecule/src/molecule_auto_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "molecule/molecule_cml_loader.h"
#include "molecule/sdf_loader.h"
#include "molecule/molecule_cdx_loader.h"
#include "molecule/inchi_wrapper.h"

using namespace indigo;

Expand Down Expand Up @@ -240,22 +241,44 @@ void MoleculeAutoLoader::_loadMolecule (BaseMolecule &mol, bool query)
_scanner->seek(pos, SEEK_SET);
}

// check for SMILES format
if (Scanner::isSingleLine(*_scanner))
{
SmilesLoader loader(*_scanner);
// check for single line formats
if (Scanner::isSingleLine(*_scanner)) {
// check for InChI format
{
char prefix[6];
int start = _scanner->tell();
_scanner->readCharsFix(6, prefix);
_scanner->seek(start, SEEK_SET);

loader.ignore_closing_bond_direction_mismatch =
ignore_closing_bond_direction_mismatch;
loader.stereochemistry_options = stereochemistry_options;
loader.ignore_cistrans_errors = ignore_cistrans_errors;
if (query)
loader.loadQueryMolecule((QueryMolecule &)mol);
else
loader.loadMolecule((Molecule &)mol);
return;
}
if (!strncmp(prefix, "InChI=", 6)) {
if (query) {
throw Error("InChI input doesn't support query molecules");
}

Array<char> inchi;
_scanner->readWord(inchi, " ");

InchiWrapper loader;
loader.loadMoleculeFromInchi(inchi.ptr(), (Molecule &)mol);
return;
}
}

// if not InChI then SMILES
{
SmilesLoader loader(*_scanner);

loader.ignore_closing_bond_direction_mismatch =
ignore_closing_bond_direction_mismatch;
loader.stereochemistry_options = stereochemistry_options;
loader.ignore_cistrans_errors = ignore_cistrans_errors;
if (query)
loader.loadQueryMolecule((QueryMolecule &)mol);
else
loader.loadMolecule((Molecule &)mol);
return;
}
}

// check for CDX format
/*
Expand Down
5 changes: 5 additions & 0 deletions molecule/src/molfile_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2986,6 +2986,8 @@ void MolfileLoader::_readSGroup3000 (const char *str)
BufferScanner scanner(str);
QS_DEF(Array<char>, type);
QS_DEF(Array<char>, entity);
entity.clear();
type.clear();

MoleculeSGroups *sgroups = &_bmol->sgroups;

Expand Down Expand Up @@ -3062,6 +3064,7 @@ void MolfileLoader::_readSGroup3000 (const char *str)
else if (strcmp(entity.ptr(), "SUBTYPE") == 0)
{
QS_DEF(Array<char>, subtype);
subtype.clear();
scanner.readWord(subtype, 0);
if (strcmp(subtype.ptr(), "ALT") == 0)
sgroup->sgroup_subtype = SGroup::SG_SUBTYPE_ALT;
Expand All @@ -3084,6 +3087,7 @@ void MolfileLoader::_readSGroup3000 (const char *str)
else if (strcmp(entity.ptr(), "BRKTYP") == 0)
{
QS_DEF(Array<char>, style);
style.clear();
scanner.readWord(style, 0);
if (strcmp(style.ptr(), "BRACKET") == 0)
sgroup->brk_style = _BRKTYP_SQUARE;
Expand Down Expand Up @@ -3137,6 +3141,7 @@ void MolfileLoader::_readSGroup3000 (const char *str)
else if (strcmp(entity.ptr(), "FIELDDISP") == 0)
{
QS_DEF(Array<char>, substr);
substr.clear();
_readStringInQuotes(scanner, &substr);
if (dsg != 0)
{
Expand Down
60 changes: 56 additions & 4 deletions 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,11 +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 _initDataSGroups();
void _initSruGroups();
void _initMulGroups();
void _initSupGroups();

void _prepareSGroups();
void _initSGroups(Tree& sgroups, Rect2f parent);
void _initSGroups();

void _findAnglesOverPi();
void _renderBondIds();
void _renderAtomIds();
Expand Down Expand Up @@ -111,6 +112,7 @@ class MoleculeRenderInternal {
int _findClosestCircle (Vec2f& p, int aid, float radius, int skip = -1);
int _findClosestBox (Vec2f& p, int aid, const Vec2f& sz, float mrg, int skip = -1);
void _preparePseudoAtom (int aid, int color, bool highlighted);
void _prepareChargeLabel(int aid, int color, bool highlighted);
void _prepareLabelText (int aid);
void _prepareAAM ();
int _pushTextItem (RenderItem::TYPE type, int color, bool highlighted);
Expand Down Expand Up @@ -140,6 +142,56 @@ 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();
if (n <= 0) {
return Rect2f(Vec2f(0, 0), Vec2f(0, 0));
}
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;
}

inline static Vec2f ILLEGAL_POINT() {
return Vec2f(nanf(""), nanf(""));
}

//TODO: eliminate
inline static Rect2f ILLEGAL_RECT () {
return Rect2f(ILLEGAL_POINT(), ILLEGAL_POINT());
}

inline static bool IS_NAN(float x) {
return x != x;
}
inline static bool IS_ILLEGAL(Vec2f point) {
return IS_NAN(point.x) && IS_NAN(point.y);
}
inline static bool IS_ILLEGAL(Rect2f rect) {
return IS_ILLEGAL(rect.leftBottom()) && IS_ILLEGAL(rect.rightTop());
}

// 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 0c72126

Please sign in to comment.