optimize the tri-ray calculation.