Skip to content

Commit

Permalink
#539 MeasureTool - linear x-axis (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
tariqksoliman authored Jun 13, 2024
1 parent e4eda47 commit e97d9be
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 24 deletions.
18 changes: 13 additions & 5 deletions src/essence/Tools/Legend/LegendTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ var LegendTool = {
this.displayOnStart = L_.getToolVars('legend')['displayOnStart']
this.justification = L_.getToolVars('legend')['justification']
if (this.justification == 'right') {
var toolController = d3.select('#toolcontroller_sepdiv')
var toolContent = d3.select('#toolContentSeparated_Legend')
const toolController = d3.select('#toolcontroller_sepdiv')
const toolContent = d3.select('#toolContentSeparated_Legend')
toolController.style('top', '110px')
toolController.style('left', null)
toolController.style('right', '5px')
toolContent.style('left', null)
toolContent.style('right', '0px')
} else if (this.justification != L_.getToolVars('identifier')['justification']) {
var toolController = d3.select('#toolcontroller_sepdiv').clone(false).attr('id', 'toolcontroller_sepdiv_left')
} else if (
this.justification != L_.getToolVars('identifier')['justification']
) {
const toolController = d3
.select('#toolcontroller_sepdiv')
.clone(false)
.attr('id', 'toolcontroller_sepdiv_left')
$('#toolSeparated_Legend').appendTo('#toolcontroller_sepdiv_left')
toolController.style('top', '40px')
toolController.style('left', '5px')
Expand Down Expand Up @@ -140,7 +145,10 @@ function drawLegendHeader() {
.style('line-height', '30px')
.style('font-size', '13px')
.style('padding-right', '8px')
.style('padding-left', '30px')
.style(
'padding-left',
LegendTool.justification === 'right' ? '10px' : '30px'
)
.style('color', 'var(--color-l)')
.style('background', 'var(--color-i)')
.style('font-family', 'lato-light')
Expand Down
166 changes: 147 additions & 19 deletions src/essence/Tools/Measure/MeasureTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ const Measure = () => {
const refLine = useRef(null)

useEffect(() => {
updateProfileData = setProfileData
updateProfileData = (d) => {
MeasureTool.uniformData = uniform(d)
setProfileData(d)
}
triggerRefresh = () => {
_refreshCounter += 1
setRefresh(_refreshCounter)
Expand Down Expand Up @@ -261,7 +264,8 @@ const Measure = () => {
<Line
ref={refLine}
data={{
labels: MeasureTool.lastData.map((d) => {
labels: MeasureTool.uniformData.map((uD) => {
const d = MeasureTool.lastData[uD.index]
const xAxes = parseInt(d[2], 10)
return distDisplayUnit === 'kilometers'
? (xAxes / 1000).toFixed(2)
Expand All @@ -270,19 +274,23 @@ const Measure = () => {
datasets: [
{
label: 'Profile',
data: profileData,
data: MeasureTool.uniformData.map((d) => d.y),
segment: {
backgroundColor: (ctx) => {
if (
LOS.on &&
MeasureTool.lineOfSight[
ctx.p1DataIndex
MeasureTool.uniformData[
ctx.p1DataIndex
].index
] === 0
)
return 'black'
const i =
MeasureTool.datasetMapping[
ctx.p0DataIndex
MeasureTool.uniformData[
ctx.p0DataIndex
].index
] - 1
if (mode === 'continuous_color') {
return MeasureTool.getColor(i, 0.4)
Expand All @@ -296,13 +304,17 @@ const Measure = () => {
if (
LOS.on &&
MeasureTool.lineOfSight[
ctx.p1DataIndex
MeasureTool.uniformData[
ctx.p1DataIndex
].index
] === 0
)
alpha = 0.75
const i =
MeasureTool.datasetMapping[
ctx.p0DataIndex
MeasureTool.uniformData[
ctx.p0DataIndex
].index
] - 1
if (mode === 'continuous_color')
return MeasureTool.getColor(
Expand Down Expand Up @@ -364,6 +376,32 @@ const Measure = () => {
},
},
scales: {
x: {
type: 'linear',
min: 0,
max:
MeasureTool.lastData.length > 1
? parseInt(
MeasureTool.lastData[
MeasureTool.lastData.length -
1
][2]
) *
(distDisplayUnit === 'kilometers'
? 0.001
: 1)
: 1,
ticks: {
autoSkip: false,
stepSize: getIdealXAxisStepSize(),
callback: function (value, index, values) {
if (distDisplayUnit === 'kilometers') {
return value.toFixed(2) + 'km'
}
return value + 'm'
},
},
},
y: {
grid: {
color: 'rgba(255,255,255,0.05)',
Expand All @@ -379,16 +417,30 @@ const Measure = () => {
onHover: (e, el) => {
let d
let visible = '--'
if (refLine && e.x != null) {
if (
refLine &&
e.x != null &&
MeasureTool.uniformData.length > 2
) {
const chartArea = refLine.current.chartArea
const yScale = refLine.current.scales.y
const bestIndex = Math.round(
F_.linearScale(
[chartArea.left, chartArea.right],
[0, profileData.length],
e.x
)
)
let bestIndex =
MeasureTool.uniformData[
Math.round(
F_.linearScale(
[
chartArea.left,
chartArea.right,
],
[0, profileData.length],
e.x
)
)
]
if (bestIndex != null)
bestIndex = bestIndex.index
else return

$('#measureVerticalCursor').css({
left: `${e.x}px`,
height: `${chartArea.bottom}px`,
Expand Down Expand Up @@ -588,6 +640,7 @@ let MeasureTool = {
data: [],
lineOfSight: [],
lastData: [],
uniformData: [],
mapFocusMarker: null,
dems: [],
activeDemIdx: 0,
Expand Down Expand Up @@ -752,6 +805,7 @@ let MeasureTool = {
updateProfileData(profileData)
MeasureTool.data = []
MeasureTool.lastData = []
MeasureTool.uniformData = []
MeasureTool.datasetMapping = []
MeasureTool.clearFocusPoint()
Map_.rmNotNull(distLineToMouse)
Expand Down Expand Up @@ -920,8 +974,9 @@ let MeasureTool = {
profileData = []
MeasureTool.data = []
MeasureTool.lastData = []
MeasureTool.uniformData = []
MeasureTool.datasetMapping = []
distDisplayUnit = 'meters'
//distDisplayUnit = 'meters'

Map_.rmNotNull(distLineToMouse)
Map_.rmNotNull(distMousePoint)
Expand Down Expand Up @@ -1048,6 +1103,80 @@ function recomputeLineOfSight() {
}
}

// Takes non-linear x-axis profileData and makes it fit linearly. (Only for display purposes)
function uniform(d) {
const linearProfileData = []

if (MeasureTool.lastData.length > 2 && d.length != 0) {
const totalDistance =
MeasureTool.lastData[MeasureTool.lastData.length - 1][2]
const steps = MeasureTool.lastData.length
const step = totalDistance / steps
let workingDistance = 0

let currentLastDataI = 1

for (let i = 0; i < steps; i++) {
while (
MeasureTool.lastData[currentLastDataI][2] < workingDistance
) {
currentLastDataI++
}

linearProfileData.push({
y: yFromX(
{
x: MeasureTool.lastData[currentLastDataI - 1][2],
y: MeasureTool.lastData[currentLastDataI - 1][4],
},
{
x: MeasureTool.lastData[currentLastDataI][2],
y: MeasureTool.lastData[currentLastDataI][4],
},
workingDistance
),
index: currentLastDataI - 1,
})

workingDistance += step
}
}
return linearProfileData
}
function yFromX(point1, point2, x) {
const gradient = (point2.y - point1.y) / (point2.x - point1.x)
const intercept = point1.y - gradient * point1.x
return gradient * x + intercept
}

function getIdealXAxisStepSize() {
let stepSize = 0.1

if (MeasureTool.lastData.length >= 2) {
const totalDistance =
MeasureTool.lastData[MeasureTool.lastData.length - 1][2]

if (totalDistance > 1) stepSize = 0.2
if (totalDistance > 10) stepSize = 1
if (totalDistance > 50) stepSize = 2
if (totalDistance > 100) stepSize = 10
if (totalDistance > 500) stepSize = 20
if (totalDistance > 1000) stepSize = 100
if (totalDistance > 5000) stepSize = 200
if (totalDistance > 10000) stepSize = 1000
if (totalDistance > 50000) stepSize = 2000
if (totalDistance > 100000) stepSize = 10000
if (totalDistance > 500000) stepSize = 20000
if (totalDistance > 1000000) stepSize = 100000
if (totalDistance > 5000000) stepSize = 200000
if (totalDistance > 10000000) stepSize = 1000000
if (totalDistance > 50000000) stepSize = 2000000
if (totalDistance > 100000000) stepSize = 1000000
if (totalDistance > 500000000) stepSize = 2000000
}
return stepSize
}

function makeMeasureToolLayer() {
Map_.rmNotNull(measureToolLayer)

Expand Down Expand Up @@ -1341,11 +1470,10 @@ function makeProfile() {
}
//profileData = profileData.concat(data);
//var latestDistPerStep = latLongDistBetween(elevPoints[0].y, elevPoints[0].x, elevPoints[1].y, elevPoints[1].x) / steps;
var usedData = profileData
//var usedData = profileData
//if(profileMode == "slope") {
// usedData = elevsToSlope
var multiplyElev = MeasureTool.vars.multiplyelev || 1

//var multiplyElev = MeasureTool.vars.multiplyelev || 1
updateProfileData(profileData)

Globe_.litho.removeLayer('_measurePoint')
Expand Down

0 comments on commit e97d9be

Please sign in to comment.