There is one who says that post facto, if they recited Keriat Shema even without the separation of a garment, and face to face, and even with another who is not his wife—they have fulfilled their obligation (Pri Chadash). And there are those who doubt this (Pri Megadim). And it appears the primary view is like the first opinion, for we have not found that one must repeat the recitation except when one needs to relieve himself, for "the sacrifice of the wicked is an abomination," or in a place where there was excrement, as I wrote in siman 76, for it is a greater disgrace. But in this case, where it is a mere concern, a concern of improper thought—certainly he does not need to repeat the recitation. And it appears to me further that even if his heart sees the nakedness—he does not need to repeat the recitation. Therefore, we have not found a revelation of these laws in the Gemara or the poskim, that one must repeat the recitation or pray. And proof for this: for here, thought is permitted in matters of Torah, and likewise when his heart sees the nakedness, as the great Acharonim wrote (Magen Avraham siman 85, subsection 2, and Pri Chadash at the end of siman 75). This is not the case in filthy places, and certainly when one needs to relieve himself. And since thought is permitted, it follows that post facto, if he recited Keriat Shema or prayed—he does not need to repeat (so it appears according to my humble opinion).
יש מי שאומר דבדיעבד כשקראו קריאת שמע אף בלא הפסקת טלית, ופנים כנגד פנים, ואף באחר שלא באשתו – יצאו ידי חובתן (פרי חדש). ויש מפקפקין בזה (פרי מגדים). ונראה עיקר כדעה ראשונה, דלא מצינו שיהא צריך לחזור ולקרות אלא בצריך לנקביו ד"זבח רשעים תועבה", או במקום שהיה בו צואה כמו שכתבתי בסימן ע"ו דהוי בזיון טפי. אבל בזה דחששא בעלמא הוא חששא דהרהורא – וודאי דאינו צריך לחזור ולקרות. ונראה לי עוד דאפילו בלבו רואה את הערוה – אינו צריך לחזור ולקרות. ולכן לא מצינו גילוי לדינים אלו בגמרא ובפוסקים, שיהא צריך לחזור ולקרות או להתפלל. וראיה לזה: דהא בכאן הרהור מותר בדברי תורה, וכן בלבו רואה את הערוה, כמו שכתבו גדולי אחרונים (מגן אברהם סימן פ"ה סעיף קטן ב', ופרי חדש סוף סימן ע"ה). מה שאין כן במקומות המטונפים, וכל שכן כשצריך לנקביו. וכיון דהרהור מותר, ממילא דבדיעבד כשקרא קריאת שמע או התפלל – אינו צריך לחזור (כן נראה לפי עניות דעתי).