Skip to content

Commit

Permalink
#225 – moved CIP rendering logic to Bond class
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitvex committed Apr 12, 2023
1 parent c608227 commit c9e4926
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 34 deletions.
31 changes: 2 additions & 29 deletions packages/ketcher-core/src/application/render/restruct/reatom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,10 @@ class ReAtom extends ReObject {
const path = paper.set()

const cipLabelPosition = atom.pp.scaled(options.scale)
const cipValuePath = getCIPValuePath({
const cipValuePath = util.getCIPValuePath({
paper,
cipLabelPosition,
atom,
atomOrBond: atom,
options
})
const box = util.relBox(cipValuePath.getBBox())
Expand All @@ -429,33 +429,6 @@ class ReAtom extends ReObject {
}
}

function getCIPValuePath({
paper,
cipLabelPosition,
atom,
options
}: {
paper
cipLabelPosition: Vec2
atom: Atom
options
}) {
const text = paper
.text(cipLabelPosition.x, cipLabelPosition.y, `(${atom.cip})`)
.attr({
font: options.font,
'font-size': options.fontsz
})
const box = text.getBBox()
const path = paper.set()
const rect = paper
.rect(box.x - 1, box.y - 1, box.width + 2, box.height + 2, 3, 3)
.attr({ fill: '#0f0', stroke: '#fff', opacity: 1 })
path.push(rect.toBack(), text.toBack())

return path
}

function getStereoAtomColor(options, stereoLabel) {
if (
!stereoLabel ||
Expand Down
28 changes: 27 additions & 1 deletion packages/ketcher-core/src/application/render/restruct/rebond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ReBond extends ReObject {
// eslint-disable-line max-statements
const render = restruct.render
const struct = restruct.molecule
const bond = restruct.molecule.bonds.get(bid)
const bond = restruct.molecule.bonds.get(bid)!
const sgroups = restruct.molecule.sgroups
const functionalGroups = restruct.molecule.functionalGroups
if (
Expand Down Expand Up @@ -246,6 +246,32 @@ class ReBond extends ReObject {
true
)
}

if (bond.cip) {
this.drawCIPLabel(bond, restruct)
}

this.path.toBack()
}

drawCIPLabel(bond: Bond, restruct: ReStruct) {
const render = restruct.render
const { options, paper } = render
const path = paper.set()

const cipLabelPosition = bond.center.scaled(options.scale)
const cipValuePath = util.getCIPValuePath({
paper,
cipLabelPosition,
atomOrBond: bond,
options
})
const box = util.relBox(cipValuePath.getBBox())

cipValuePath.translateAbs(0.5 * box.width, -0.5 * box.height)
path.push(cipValuePath.toBack())

restruct.addReObjectPath(LayerMap.data, this.visel, path, null, true)
}
}

Expand Down
32 changes: 30 additions & 2 deletions packages/ketcher-core/src/application/render/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
***************************************************************************/

import { Vec2 } from 'domain/entities'
import { Atom, Bond, Vec2 } from 'domain/entities'
import assert from 'assert'

function tfx(v) {
Expand Down Expand Up @@ -127,11 +127,39 @@ function calcCoordinates(aPoint, bPoint, lengthHyp) {
return obj
}

function getCIPValuePath({
paper,
cipLabelPosition,
atomOrBond,
options
}: {
paper
cipLabelPosition: Vec2
atomOrBond: Atom | Bond
options
}) {
const text = paper
.text(cipLabelPosition.x, cipLabelPosition.y, `(${atomOrBond.cip})`)
.attr({
font: options.font,
'font-size': options.fontsz
})
const box = text.getBBox()
const path = paper.set()
const rect = paper
.rect(box.x - 1, box.y - 1, box.width + 2, box.height + 2, 3, 3)
.attr({ fill: '#fff', stroke: '#fff', opacity: 1 })
path.push(rect.toBack(), text.toBack())

return path
}

const util = {
tfx,
relBox,
shiftRayBox,
calcCoordinates
calcCoordinates,
getCIPValuePath
}

export default util
2 changes: 1 addition & 1 deletion packages/ketcher-core/src/domain/entities/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export enum StereoLabel {
Or = 'or'
}

export enum CIP {
enum CIP {
S = 'S',
R = 'R',
s = 's',
Expand Down
13 changes: 12 additions & 1 deletion packages/ketcher-core/src/domain/entities/bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import { Pile } from './pile'
import { Struct } from './struct'
import { Vec2 } from './vec2'

enum CIP {
E = 'E',
Z = 'Z',
e = 'e',
z = 'z'
}

export interface BondAttributes {
reactingCenterStatus?: number
topology?: number
Expand All @@ -26,6 +33,7 @@ export interface BondAttributes {
type: number
end: number
begin: number
cip?: CIP | null
}

export class Bond {
Expand Down Expand Up @@ -72,7 +80,8 @@ export class Bond {
type: Bond.PATTERN.TYPE.SINGLE,
stereo: Bond.PATTERN.STEREO.NONE,
topology: Bond.PATTERN.TOPOLOGY.EITHER,
reactingCenterStatus: Bond.PATTERN.REACTING_CENTER.UNMARKED
reactingCenterStatus: Bond.PATTERN.REACTING_CENTER.UNMARKED,
cip: null
}

begin: number
Expand All @@ -85,6 +94,7 @@ export class Bond {
len: number
sb: number
sa: number
cip?: CIP | null
hb1?: number
hb2?: number
angle: number
Expand All @@ -98,6 +108,7 @@ export class Bond {
this.stereo = Bond.PATTERN.STEREO.NONE
this.topology = Bond.PATTERN.TOPOLOGY.EITHER
this.reactingCenterStatus = 0
this.cip = attributes.cip ?? null
this.len = 0
this.sb = 0
this.sa = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export function bondToStruct(source) {
ifDef(params, 'topology', source.topology)
ifDef(params, 'reactingCenterStatus', source.center)
ifDef(params, 'stereo', source.stereo)
ifDef(params, 'cip', source.cip)
// if (params.stereo)
// params.stereo = params.stereo > 1 ? params.stereo * 2 : params.stereo;
// params.xxx = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ export const bond = {
'Made/broken and changes'
], // "Order changes" x 3
default: 0
},
cip: {
title: 'CIP',
type: 'string',
enum: ['E', 'Z', 'e', 'z']
}
}
}
Expand Down

0 comments on commit c9e4926

Please sign in to comment.