/// <summary> /// Creates a line starting at the projection of this.loc on the intersection between this and another Plane. /// The direction of the line is that of the crossproduct of this.normal and anotherPlane.normal. /// </summary> /// <returns>A line along the intersection of this and anotherPlane, returns "null" if two planes are significantly parallel</returns> /// <param name="anotherPlane">A plane to calculate the intersection with</param> public Scientrace.Line getIntersectionLineWith(Scientrace.Plane anotherPlane) { Scientrace.UnitVector n1, n2; n1 = this.getNorm(); n2 = anotherPlane.getNorm(); Vector tw = n1.crossProduct(n2); if (tw.length < MainClass.SIGNIFICANTLY_SMALL) { //Console.WriteLine("WARNING: two planes do not intersect since they are parallel."); return(null); } UnitVector intersection_direction = tw.tryToUnitVector(); Vector tl1 = n1.crossProduct(intersection_direction); if (tl1.length == 0) { throw new Exception("Plane.getIntersectionLineWith/l1.length can never be zero. If this happens, notify the [email protected] about this..." + this.ToString() + anotherPlane.ToString()); } // l1 lies in "this" plane, and is orthogonal to the intersectionline-direction UnitVector l1 = tl1.tryToUnitVector(); // where l1, starting at this.loc, meets anotherPlane we found a location on the intersectionline. Scientrace.Location retLoc = anotherPlane.lineThroughPlane(new Line(this.loc, l1)); if (retLoc == null) { Console.WriteLine("WARNING: retLoc == null; l1: " + l1.ToString() + " ; anotherPlane:" + anotherPlane.getNorm().ToString()); return(null); } return(new Line(retLoc, intersection_direction)); }
} // end func partialReflectRefract public void initCreatedRefractTrace(Trace refractTrace, UnitVector surfaceNormal, Scientrace.Object3d fromObject3d, Scientrace.Object3d toObject3d) { double oldrefindex = fromObject3d.materialproperties.refractiveindex(this); double newrefindex = toObject3d.materialproperties.refractiveindex(this); //for definitions on the parameters below, check fullInternalReflects function. UnitVector incoming_trace_direction = this.traceline.direction; if (incoming_trace_direction.dotProduct(surfaceNormal) > 0) { surfaceNormal = surfaceNormal.negative(); } Scientrace.UnitVector nnorm = surfaceNormal.negative(); Scientrace.Vector incoming_normal_projection = nnorm * (incoming_trace_direction.dotProduct(nnorm)); Vector L1 = incoming_trace_direction - incoming_normal_projection; double L2 = (oldrefindex / newrefindex) * L1.length; if (incoming_trace_direction == incoming_normal_projection) { //in case of normal incident light: do not refract. refractTrace.traceline.direction = incoming_trace_direction; return; } try { refractTrace.traceline.direction = ((nnorm * Math.Sqrt(1 - Math.Pow(L2, 2))) + (L1.tryToUnitVector() * L2)).tryToUnitVector(); } catch (ZeroNonzeroVectorException) { Console.WriteLine("WARNING: cannot define direction for refraction trace. Using surface normal instead. (L1: " + incoming_trace_direction.trico() + ", L2:" + incoming_normal_projection.trico() + ")."); refractTrace.traceline.direction = nnorm; } //end try/catch }
/// <summary> /// When two vectors are parallel, the cross product cannot be normalised. This method hacks around that problem. /// </summary> /// <returns>A UnitVector</returns> /// <param name="aVector">A second vector to which the result will be orthogonal.</param> public UnitVector safeNormalisedCrossProduct(Scientrace.Vector aVector) { if (this.length * aVector.length == 0) { return(UnitVector.x1vector()); } Vector crossVector = this.crossProduct(aVector); if (crossVector.length == 0) { return(this.safeNormalisedCrossProduct(aVector.vectorDance())); } return(crossVector.tryToUnitVector()); }