Skip to content

Commit

Permalink
ket rdf
Browse files Browse the repository at this point in the history
  • Loading branch information
even1024 committed Nov 18, 2024
1 parent 4991bb0 commit 952f132
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 49 deletions.
1 change: 1 addition & 0 deletions api/c/indigo/src/indigo_reaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ IndigoReaction::~IndigoReaction()
void IndigoReaction::init(std::unique_ptr<BaseReaction>&& reaction)
{
rxn = reaction ? std::move(reaction) : std::make_unique<Reaction>();
_properties.copy(rxn->properties());
}

Reaction& IndigoReaction::getReaction()
Expand Down
6 changes: 3 additions & 3 deletions core/indigo-core/layout/pathway_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace indigo
: _reaction(reaction), _depths(MAX_DEPTHS, 0), _maxDepth(0), _bond_length(options.DEFAULT_BOND_LENGTH),
_default_arrow_size((float)options.DEFAULT_BOND_LENGTH * ARROW_LENGTH_FACTOR),
_reaction_margin_size(options.reactionComponentMarginSize / options.ppi), _preserve_molecule_layout(true),
_text_height((float)options.DEFAULT_BOND_LENGTH * TEXT_LINE_HEIGHT)
_text_line_height((float)options.DEFAULT_BOND_LENGTH * TEXT_LINE_HEIGHT)
{
}

Expand Down Expand Up @@ -108,7 +108,7 @@ namespace indigo
mol.scale(center, bondLength / mean);
}
Rect2f boundingBox;
mol.getBoundingBox(boundingBox);
mol.getBoundingBox(boundingBox, Vec2f(bondLength, bondLength));
molecules.push_back(std::make_pair(reactantIdx, boundingBox));
width = boundingBox.width();
height = boundingBox.height();
Expand Down Expand Up @@ -263,7 +263,7 @@ namespace indigo
std::vector<PathwayLayoutRootItem> _layoutRootItems;

const float _bond_length;
const float _text_height;
const float _text_line_height;
const float _default_arrow_size;
const float _reaction_margin_size;
bool _preserve_molecule_layout;
Expand Down
24 changes: 14 additions & 10 deletions core/indigo-core/layout/src/pathway_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,18 @@ void PathwayLayout::buildLayoutTree()

auto totalHeight = std::accumulate(currentLayoutItem.children.begin(), currentLayoutItem.children.end(), 0.0f,
[](float summ, const PathwayLayoutItem* item) { return summ + item->height; });
auto totalSpacing = (currentLayoutItem.children.size() - 1) * VERTICAL_SPACING;
totalHeight -= currentLayoutItem.children.front()->height / 2;
totalHeight -= currentLayoutItem.children.back()->height / 2;
totalHeight /= 2.0f;
float targetHeight = totalLines * _text_height + _reaction_margin_size;
float targetHeight = totalLines * _text_line_height + _reaction_margin_size;
if (totalHeight < targetHeight)
{
auto adjustHeight = (targetHeight - totalHeight) / (currentLayoutItem.children.size() - 1);
for (auto& child : currentLayoutItem.children)
child->height += adjustHeight * TEXT_ADJUSTMENT; // should be 2.0f
{
child->height *= (targetHeight - totalSpacing / 2);
child->height /= totalHeight;
}
}
}
}
Expand Down Expand Up @@ -197,7 +200,7 @@ void PathwayLayout::copyTextPropertiesToNode(const PathwayReaction::SimpleReacti
}
}

if (node.name_text.size())
if (node.name_text.size() && node.conditions_text.size())
node.name_text.push().readString("", true);
}

Expand Down Expand Up @@ -306,7 +309,7 @@ void PathwayLayout::applyLayout()
arrows.insert(arrows.end(), tails.begin(), tails.end());

// add spines
float text_height_limit = MIN_LINES_COUNT * _text_height;
float text_height_limit = MIN_LINES_COUNT * _text_line_height;
auto& node = _reaction.getReactionNode(layoutItem->reactionIndex);
Vec2f textPos_bl(0, head.y);
if (tails.size() > 1)
Expand Down Expand Up @@ -341,12 +344,12 @@ void PathwayLayout::generateTextBlocks(SimpleTextObjectBuilder& tob, const ObjAr
{
for (int i = 0; i < props.size(); ++i)
{
if (height > _text_height)
if (std::ceil(height * 1000) >= std::ceil(_text_line_height * 1000))
{
height -= _text_height;
height -= _text_line_height;
SimpleTextLine textLine;
textLine.text = props[i].ptr();
if (height < _text_height && props.size() - i > 1)
if (std::ceil(height * 1000) < std::ceil(_text_line_height * 1000) && props.size() - i > 1)
textLine.text += "...";
auto& ts = textLine.text_styles.emplace_back();
ts.offset = 0;
Expand All @@ -365,8 +368,9 @@ void PathwayLayout::addMetaText(PathwayReaction::ReactionNode& node, const Vec2f
generateTextBlocks(tob, node.name_text, KFontBoldStr, height_limit);
generateTextBlocks(tob, node.conditions_text, KFontItalicStr, height_limit);
tob.finalize();
Vec3f text_pos_lr(text_pos_bl.x, text_pos_bl.y + _text_height / 2 + (text_height_limit - height_limit), 0.0f);
_reaction.meta().addMetaObject(new SimpleTextObject(text_pos_lr, tob.getJsonString()), true);
auto text_height = _text_line_height / 2 + (text_height_limit - height_limit);
Vec3f text_pos_tr(text_pos_bl.x, text_pos_bl.y + text_height, 0.0f);
_reaction.meta().addMetaObject(new SimpleTextObject(text_pos_tr, Vec2f(node.text_width, text_height), tob.getJsonString()), true);
}

std::vector<std::string> PathwayLayout::splitText(const std::string& text, float max_width, std::function<float(char ch)> symbol_width)
Expand Down
7 changes: 4 additions & 3 deletions core/indigo-core/molecule/meta_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,16 @@ namespace indigo

static const std::uint32_t CID = "Simple text object"_hash;

SimpleTextObject(const Vec3f& pos, const std::string& content);
SimpleTextObject(const Vec3f& pos, const Vec2f& sz, const std::string& content);

MetaObject* clone() const override
{
return new SimpleTextObject(_pos, _content);
return new SimpleTextObject(_pos, _size, _content);
}

void getBoundingBox(Rect2f& bbox) const override
{
bbox = Rect2f(Vec2f(_pos.x, _pos.y), Vec2f(_pos.x, _pos.y));
bbox = Rect2f(Vec2f(_pos.x, _pos.y), Vec2f(_pos.x + _size.x, _pos.y - _size.y));
}

void offset(const Vec2f& offset) override
Expand All @@ -185,6 +185,7 @@ namespace indigo
std::string _content;
std::list<SimpleTextLine> _block;
Vec3f _pos;
Vec2f _size;
};

class SimpleTextObjectBuilder
Expand Down
14 changes: 12 additions & 2 deletions core/indigo-core/molecule/molecule_cdxml_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ namespace indigo
bool is_superatom;
};

struct CdxmlText
{
CdxmlText(const Vec3f& pos, const Vec2f& size, const std::string& text) : pos(pos), size(size), text(text)
{
}
std::string text;
Vec3f pos;
Vec2f size;
};

class BaseCDXProperty
{
public:
Expand Down Expand Up @@ -805,7 +815,7 @@ namespace indigo
std::vector<CdxmlNode> nodes;
std::vector<CdxmlBond> bonds;
std::vector<CdxmlBracket> brackets;
std::vector<std::pair<Vec3f, std::string>> text_objects;
std::vector<CdxmlText> text_objects;

static const int SCALE = 30;

Expand All @@ -820,7 +830,7 @@ namespace indigo
void _parseBond(CdxmlBond& bond, BaseCDXProperty& prop);

void _parseBracket(CdxmlBracket& bracket, BaseCDXProperty& prop);
void _parseText(BaseCDXElement& elem, std::vector<std::pair<Vec3f, std::string>>& text_parsed);
void _parseText(BaseCDXElement& elem, std::vector<CdxmlText>& text_parsed);
void _parseLabel(BaseCDXElement& elem, std::string& label);

void _parseGraphic(BaseCDXElement& elem);
Expand Down
2 changes: 2 additions & 0 deletions core/indigo-core/molecule/molecule_json_saver.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ namespace indigo
int getMonomerNumber(int mon_idx);

void writeFloat(JsonWriter& writer, float f_value);
void writePos(JsonWriter& writer, const Vec3f& pos);

void saveAtoms(BaseMolecule& mol, JsonWriter& writer);
void saveBonds(BaseMolecule& mol, JsonWriter& writer);
void saveRGroup(PtrPool<BaseMolecule>& fragments, int rgnum, JsonWriter& writer);
Expand Down
3 changes: 2 additions & 1 deletion core/indigo-core/molecule/src/meta_commons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ namespace indigo
return base64::encode(_image_data.c_str(), _image_data.size());
}

SimpleTextObject::SimpleTextObject(const Vec3f& pos, const std::string& content) : MetaObject(CID)
SimpleTextObject::SimpleTextObject(const Vec3f& pos, const Vec2f& sz, const std::string& content) : MetaObject(CID)
{
using namespace rapidjson;
_pos = pos;
_size = sz;
_content = content;
Document data;
data.Parse(content.c_str());
Expand Down
7 changes: 3 additions & 4 deletions core/indigo-core/molecule/src/molecule_cdxml_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ void MoleculeCdxmlLoader::_parseCollections(BaseMolecule& mol)
_addBracket(mol, brk);

for (const auto& to : text_objects)
mol.meta().addMetaObject(new SimpleTextObject(to.first, to.second));
mol.meta().addMetaObject(new SimpleTextObject(to.pos, to.size, to.text));

for (const auto& plus : _pluses)
mol.meta().addMetaObject(new ReactionPlusObject(plus));
Expand Down Expand Up @@ -1683,7 +1683,7 @@ void MoleculeCdxmlLoader::_parseLabel(BaseCDXElement& elem, std::string& label)
}
}

void MoleculeCdxmlLoader::_parseText(BaseCDXElement& elem, std::vector<std::pair<Vec3f, std::string>>& text_parsed)
void MoleculeCdxmlLoader::_parseText(BaseCDXElement& elem, std::vector<CdxmlText>& text_parsed)
{
Vec3f text_pos;
auto text_coordinates_lambda = [&text_pos, this](const std::string& data) { this->parsePos(data, text_pos); };
Expand Down Expand Up @@ -1816,8 +1816,7 @@ void MoleculeCdxmlLoader::_parseText(BaseCDXElement& elem, std::vector<std::pair
std::string txt = s.GetString();
if (!is_valid_utf8(txt))
txt = latin1_to_utf8(txt);

text_parsed.emplace_back(tpos, txt.c_str());
text_parsed.emplace_back(tpos, Vec2f(text_bbox.width(), text_bbox.height()), txt.c_str());
}

void MoleculeCdxmlLoader::_parseBracket(CdxmlBracket& bracket, BaseCDXProperty& prop)
Expand Down
14 changes: 13 additions & 1 deletion core/indigo-core/molecule/src/molecule_json_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,7 +1867,19 @@ void MoleculeJsonLoader::loadMetaObjects(rapidjson::Value& meta_objects, MetaDat
text_origin.x = sobj["position"]["x"].GetFloat();
text_origin.y = sobj["position"]["y"].GetFloat();
text_origin.z = sobj["position"]["z"].GetFloat();
meta_interface.addMetaObject(new SimpleTextObject(text_origin, content));
Vec2f text_size;
if (sobj.HasMember("pos"))
{
auto pos = sobj["pos"].GetArray();
if (pos.Size())
{
Vec2f lt(pos[0]["x"].GetFloat(), pos[0]["y"].GetFloat());
Vec2f rb(pos[2]["x"].GetFloat(), pos[2]["y"].GetFloat());
text_size.x = rb.x - lt.x;
text_size.y = lt.y - rb.y;
}
}
meta_interface.addMetaObject(new SimpleTextObject(text_origin, text_size, content));
}
}
}
Expand Down
35 changes: 26 additions & 9 deletions core/indigo-core/molecule/src/molecule_json_saver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,18 @@ void MoleculeJsonSaver::writeFloat(JsonWriter& writer, float f_value)
writer.Double(f_value);
}

void indigo::MoleculeJsonSaver::writePos(JsonWriter& writer, const Vec3f& pos)
{
writer.StartObject();
writer.Key("x");
writeFloat(writer, pos.x);
writer.Key("y");
writeFloat(writer, pos.y);
writer.Key("z");
writeFloat(writer, pos.z);
writer.EndObject();
}

void MoleculeJsonSaver::saveAtoms(BaseMolecule& mol, JsonWriter& writer)
{
QS_DEF(Array<char>, buf);
Expand Down Expand Up @@ -1609,8 +1621,8 @@ void MoleculeJsonSaver::saveMolecule(BaseMolecule& bmol, JsonWriter& writer)
}
// location
writer.Key("position");
writer.StartObject();
const auto& pos = mol->getAtomXyz(i);
writer.StartObject();
writer.Key("x");
writeFloat(writer, pos.x);
writer.Key("y");
Expand Down Expand Up @@ -1964,14 +1976,19 @@ void MoleculeJsonSaver::saveMetaData(JsonWriter& writer, MetaDataStorage& meta)
writer.Key("content");
writer.String(simple_obj->_content.c_str());
writer.Key("position");
writer.StartObject();
writer.Key("x");
writeFloat(writer, simple_obj->_pos.x);
writer.Key("y");
writeFloat(writer, simple_obj->_pos.y);
writer.Key("z");
writeFloat(writer, simple_obj->_pos.z);
writer.EndObject(); // end position
writePos(writer, simple_obj->_pos);

writer.Key("pos");
writer.StartArray();
Vec2f pos_bbox(simple_obj->_pos.x, simple_obj->_pos.y);
writePos(writer, pos_bbox);
pos_bbox.y -= simple_obj->_size.y;
writePos(writer, pos_bbox);
pos_bbox.x += simple_obj->_size.x;
writePos(writer, pos_bbox);
pos_bbox.y += simple_obj->_size.y;
writePos(writer, pos_bbox);
writer.EndArray();
writer.EndObject(); // end data
writer.EndObject(); // end node
break;
Expand Down
3 changes: 2 additions & 1 deletion core/indigo-core/reaction/pathway_reaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace indigo
reaction->addProductCopy(*_molecules[pidx], 0, 0);
for (auto ridx : sr.reactantIndexes)
reaction->addReactantCopy(*_molecules[ridx], 0, 0);
reaction->properties().copy(sr.properties);
return reaction;
}

Expand All @@ -131,7 +132,7 @@ namespace indigo
return static_cast<int>(_molecules.size());
}

const auto& getReaction(int reaction_idx)
auto& getReaction(int reaction_idx)
{
return _reactions[reaction_idx];
}
Expand Down
4 changes: 2 additions & 2 deletions core/indigo-core/reaction/reaction_multistep_detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ namespace indigo

void constructPathwayReaction(PathwayReaction& rxn);
void detectPathwayMetadata(PathwayReaction& rxn);
void collectMetadata(PathwayReaction& rxn, const Rect2f& bbox);
void collectProperties(PathwayReaction& rxn, const SimpleTextObject& text_obj);
void collectMetadata(int reaction_idx, PathwayReaction& rxn, const Rect2f& bbox);
void collectProperties(PathwayReaction::SimpleReaction& sr, const SimpleTextObject& text_obj);

typedef std::pair<float, int> FLOAT_INT_PAIR;
typedef std::vector<FLOAT_INT_PAIR> FLOAT_INT_PAIRS;
Expand Down
Loading

0 comments on commit 952f132

Please sign in to comment.