Skip to content

Commit

Permalink
Fix surface-merging between GRIN and non-GRIN glasses
Browse files Browse the repository at this point in the history
  • Loading branch information
ricktu288 committed Apr 28, 2024
1 parent f6d0786 commit 4d9f61e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
13 changes: 13 additions & 0 deletions simulator/js/objs/circlelens.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ objTypes['circlelens'] = {

// When the obj is shot by a ray
onRayIncident: function (obj, ray, rayIndex, incidentPoint, surfaceMerging_objs) {
// If at least one of the surface merging object is a GRIN glass, the interaction should be handled by the GRIN glass instead.
if (surfaceMerging_objs) {
for (let i = 0; i < surfaceMerging_objs.length; i++) {
if (surfaceMerging_objs[i].type == 'grin_refractor' || surfaceMerging_objs[i].type == 'grin_circlelens') {
// Exclude the GRIN glass from the surface merging objects and include obj itself.
let new_surfaceMerging_objs = surfaceMerging_objs.filter((value, index, arr) => {
return value != surfaceMerging_objs[i];
});
new_surfaceMerging_objs.push(obj);
return objTypes[surfaceMerging_objs[i].type].onRayIncident(surfaceMerging_objs[i], ray, rayIndex, incidentPoint, new_surfaceMerging_objs);
}
}
}

var midpoint = geometry.segmentMidpoint(geometry.line(ray.p1, incidentPoint));
var d = geometry.distanceSquared(obj.p1, obj.p2) - geometry.distanceSquared(obj.p1, midpoint);
Expand Down
14 changes: 14 additions & 0 deletions simulator/js/objs/curvedglass.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ objTypes['curvedglass'] = {
// When the obj is shot by a ray
onRayIncident: function (obj, ray, rayIndex, incidentPoint, surfaceMerging_objs) {

// If at least one of the surface merging object is a GRIN glass, the interaction should be handled by the GRIN glass instead.
if (surfaceMerging_objs) {
for (let i = 0; i < surfaceMerging_objs.length; i++) {
if (surfaceMerging_objs[i].type == 'grin_refractor' || surfaceMerging_objs[i].type == 'grin_circlelens') {
// Exclude the GRIN glass from the surface merging objects and include obj itself.
let new_surfaceMerging_objs = surfaceMerging_objs.filter((value, index, arr) => {
return value != surfaceMerging_objs[i];
});
new_surfaceMerging_objs.push(obj);
return objTypes[surfaceMerging_objs[i].type].onRayIncident(surfaceMerging_objs[i], ray, rayIndex, incidentPoint, new_surfaceMerging_objs);
}
}
}

var incidentData = this.getIncidentData(obj, ray);
var incidentType = incidentData.incidentType;
if (incidentType == 1) {
Expand Down
8 changes: 7 additions & 1 deletion simulator/js/objs/grin_circlelens.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ objTypes['grin_circlelens'] = {
r_bodyMerging_obj = ray.bodyMerging_obj; // Save the current bodyMerging object of the ray

for (var i = 0; i < surfaceMerging_objs.length; i++) {
let p = surfaceMerging_objs[i].fn_p({ x: incidentPoint.x, y: incidentPoint.y }) // refractive index at the intersection point - incidentPoint
let p;
if (surfaceMerging_objs[i].type == "grin_circlelens" || surfaceMerging_objs[i].type == "grin_refractor") {
p = surfaceMerging_objs[i].fn_p({ x: incidentPoint.x, y: incidentPoint.y }); // refractive index at the intersection point - incidentPoint
} else {
p = surfaceMerging_objs[i].p; // non-GRIN glass
}

incidentType = objTypes[surfaceMerging_objs[i].type].getIncidentType(surfaceMerging_objs[i], ray);
if (incidentType == 1) {
// Shot from inside to outside
Expand Down
8 changes: 7 additions & 1 deletion simulator/js/objs/grin_refractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ objTypes['grin_refractor'] = {
r_bodyMerging_obj = ray.bodyMerging_obj; // Save the current bodyMerging object of the ray

for (var i = 0; i < surfaceMerging_objs.length; i++) {
let p = surfaceMerging_objs[i].fn_p({ x: incidentPoint.x, y: incidentPoint.y }) // refractive index at the intersection point - incidentPoint
let p;
if (surfaceMerging_objs[i].type == "grin_circlelens" || surfaceMerging_objs[i].type == "grin_refractor") {
p = surfaceMerging_objs[i].fn_p({ x: incidentPoint.x, y: incidentPoint.y }); // refractive index at the intersection point - incidentPoint
} else {
p = surfaceMerging_objs[i].p; // non-GRIN glass
}

incidentType = objTypes[surfaceMerging_objs[i].type].getIncidentType(surfaceMerging_objs[i], ray);
if (incidentType == 1) {
// Shot from inside to outside
Expand Down
14 changes: 14 additions & 0 deletions simulator/js/objs/halfplane.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ objTypes['halfplane'] = {

// When the obj is shot by a ray
onRayIncident: function (obj, ray, rayIndex, incidentPoint, surfaceMerging_objs) {
// If at least one of the surface merging object is a GRIN glass, the interaction should be handled by the GRIN glass instead.
if (surfaceMerging_objs) {
for (let i = 0; i < surfaceMerging_objs.length; i++) {
if (surfaceMerging_objs[i].type == 'grin_refractor' || surfaceMerging_objs[i].type == 'grin_circlelens') {
// Exclude the GRIN glass from the surface merging objects and include obj itself.
let new_surfaceMerging_objs = surfaceMerging_objs.filter((value, index, arr) => {
return value != surfaceMerging_objs[i];
});
new_surfaceMerging_objs.push(obj);
return objTypes[surfaceMerging_objs[i].type].onRayIncident(surfaceMerging_objs[i], ray, rayIndex, incidentPoint, new_surfaceMerging_objs);
}
}
}

var rdots = (ray.p2.x - ray.p1.x) * (obj.p2.x - obj.p1.x) + (ray.p2.y - ray.p1.y) * (obj.p2.y - obj.p1.y);
var ssq = (obj.p2.x - obj.p1.x) * (obj.p2.x - obj.p1.x) + (obj.p2.y - obj.p1.y) * (obj.p2.y - obj.p1.y);
var normal = { x: rdots * (obj.p2.x - obj.p1.x) - ssq * (ray.p2.x - ray.p1.x), y: rdots * (obj.p2.y - obj.p1.y) - ssq * (ray.p2.y - ray.p1.y) };
Expand Down
16 changes: 15 additions & 1 deletion simulator/js/objs/refractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,22 @@ objTypes['refractor'] = {

// When the obj is shot by a ray
onRayIncident: function (obj, ray, rayIndex, incidentPoint, surfaceMerging_objs) {

if (obj.notDone) { return; }

// If at least one of the surface merging object is a GRIN glass, the interaction should be handled by the GRIN glass instead.
if (surfaceMerging_objs) {
for (let i = 0; i < surfaceMerging_objs.length; i++) {
if (surfaceMerging_objs[i].type == 'grin_refractor' || surfaceMerging_objs[i].type == 'grin_circlelens') {
// Exclude the GRIN glass from the surface merging objects and include obj itself.
let new_surfaceMerging_objs = surfaceMerging_objs.filter((value, index, arr) => {
return value != surfaceMerging_objs[i];
});
new_surfaceMerging_objs.push(obj);
return objTypes[surfaceMerging_objs[i].type].onRayIncident(surfaceMerging_objs[i], ray, rayIndex, incidentPoint, new_surfaceMerging_objs);
}
}
}

var incidentData = this.getIncidentData(obj, ray);
var incidentType = incidentData.incidentType;
if (incidentType == 1) {
Expand Down

0 comments on commit 4d9f61e

Please sign in to comment.